akhauriyash/Code-Regression huggingface dataset:
import java.io.*;
import java.util.*;
import java.lang.*;

class Main {
	static final long M = 1000000007;

	private static long pow(long x, long n, long M) {
		long a = 1;
		while (n > 0) {
			if ((n & 1) == 1) a = (a * x) % M;
			x = (x * x) % M;
			n >>= 1;
		}
		return a;
	}

	public static void main(String[] args) {
		final FastScanner sc = new FastScanner(System.in);
		long n = sc.nextInt();
		int k = sc.nextInt();
		long[] a = new long[k + 1];
		for (int i = k; i >= 1; i--) {

			a[i] = pow(k / i, n, M);
			//a[i] = k / i;

			for (int j = 2; i * j <= k; j++) {
				a[i] = (a[i] - a[i * j] + M) % M;
				//a[i] = (a[i] - a[i * j] + M) % M;
			}
		}

		long answer = 0;
		for (int i = 1; i <= k; i++) {
			answer = (answer + a[i] * i) % M;
		}

		System.out.println(answer);
	}

	static class FastScanner {
		private final InputStream in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		public FastScanner(InputStream source) {
			in = source;
		}

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte()) return buffer[ptr++];
			else return -1;
		}

		private static boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		public boolean hasNext() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
			return hasNextByte();
		}

		public String next() {
			if (!hasNext()) throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext()) throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			long nl = nextLong();
			if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
				throw new NumberFormatException();
			return (int) nl;
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}


Measured memory_bytes: 33484
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

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

int main() {
    int n, m;
    while(cin >> m >> n, n){
        vector<vector<int>> d1(n, vector<int>(n, INF<int>));
        for (int i = 0; i < n; ++i) d1[i][i] = 1;
        auto d2 = d1;
        for (int i = 0; i < m; ++i) {
            int a, b, c, d;
            cin >> a >> b >> c >> d;
            a--; b--;
            chmin(d1[a][b], c); chmin(d1[b][a], c);
            chmin(d2[a][b], d); chmin(d2[b][a], d);
        }
        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    chmin(d1[i][j], d1[i][k]+d1[k][j]);
                }
            }
        }
        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    chmin(d2[i][j], d2[i][k]+d2[k][j]);
                }
            }
        }
        int q;
        cin >> q;
        for (int i = 0; i < q; ++i) {
            int x, y, r;
            cin >> x >> y >> r;
            if(r == 0){
                cout << d1[x-1][y-1] << "\n";
            }else {
                cout << d2[x-1][y-1] << "\n";
            }
        }
    }
    return 0;
}


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

long long MOD = 1e9+7;
int MAX_INT = 1e9;

// 最大公約数
long long gcd(long long a, long long b){
  if(b == 0) return a;
  return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) {
  return a / gcd(a, b) * b;
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long X, K, D;
  cin >> X >> K >> D;

  X = abs(X);

  long long num = abs(X) / D;

  if(K >= num) {
    K -= num;
    X -= num * D;
  }
  else {
    X -= K * D;
    K = 0;
  }

  for (K; K > 0; --K) {
    if(X <= 0) {
      break;
    }
    X -= D;
  }

  if(K % 2 == 0) {
    cout << abs(X) << endl;
  }
  else {
    cout << min(abs(X - D), abs(X + D)) << endl;
  }

  return 0;
}


Measured memory_bytes: 3664
N = int(input())
t, x, y = [0], [0], [0]
for i in range(N):
    ti, xi, yi = map(int, input().split())
    t.append(ti)
    x.append(xi)
    y.append(yi)
flag = 1
for i in range(N):
    if flag == 1:
        dt = t[i+1]-t[i]
        dx = x[i+1]-x[i]
        dy = y[i+1]-y[i]
        if (dt+dx+dy)%2 != 0 or (dt+dx+dy) < 0:
            flag = 0
        if (dt-dx-dy)%2 != 0 or (dt-dx-dy) < 0:
            flag = 0


if flag == 1:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 11728
#include <bits/stdc++.h>
using namespace std;
#define MD 1000000007
typedef long long int ll;
typedef pair<ll, ll> P;
template <typename T>
std::string tostr(const T &t){std::ostringstream os;os << t;return os.str();}
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};

int main()
{
    int n,m;
    cin>>n>>m;
    vector<ll> v1(n),v2(n),v3(n),v4(n),v5(n),v6(n),v7(n),v8(n);
    for(int i=0;i<n;i++){
        ll x,y,z;
        cin>>x>>y>>z;
        v1[i]=x+y+z;
        v2[i]=x+y-z;
        v3[i]=x-y+z;
        v4[i]=x-y-z;
        v5[i]=-x+y+z;
        v6[i]=-x+y-z;
        v7[i]=-x-y+z;
        v8[i]=-x-y-z;
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    sort(v3.begin(),v3.end());
    sort(v4.begin(),v4.end());
    sort(v5.begin(),v5.end());
    sort(v6.begin(),v6.end());
    sort(v7.begin(),v7.end());
    sort(v8.begin(),v8.end());
    ll t1=0,t2=0,t3=0,t4=0,t5=0,t6=0,t7=0,t8=0;
    for(int i=0;i<m;i++){
        t1+=v1[n-1-i];
        t2+=v2[n-1-i];
        t3+=v3[n-1-i];
        t4+=v4[n-1-i];
        t5+=v5[n-1-i];
        t6+=v6[n-1-i];
        t7+=v7[n-1-i];
        t8+=v8[n-1-i];
    }
    cout<<max(t1,max(t2,max(t3,max(t4,max(t5,max(t6,max(t7,t8)))))))<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  
  int x, y, dist, ans;
  cin >> x >> y;
  if(y%2) cout << "No" << endl;
  else{
    dist = y - x*2;
    ans = dist/2;
    if(dist >= 0){
      if(ans <= x) cout << "Yes" << endl;
      else cout << "No" << endl;
    }
    else cout << "No" << endl;
  }
  
return 0;
}

Measured memory_bytes: 3584
#include<iostream>
#include<atcoder/mincostflow>
using namespace std;
using namespace atcoder;
int H,W;
string S[50];
main()
{
	cin>>H>>W;
	for(int i=0;i<H;i++)
	{
		cin>>S[i];
	}
	mcf_graph<int,int>P(H*W+2);
	int s=H*W,t=H*W+1;
	int fst=0;
	int cnt=0;
	const int inf=1e5;
	for(int i=0;i<H;i++)for(int j=0;j<W;j++)
	{
		if(S[i][j]=='o')
		{
			P.add_edge(s,i*W+j,1,0);
			fst+=i+j;
			cnt++;
		}
		if(S[i][j]!='#')for(int r=0;r<2;r++)
		{
			int x=i+r,y=j+1-r;
			if(x<H&&y<W&&S[x][y]!='#')P.add_edge(i*W+j,x*W+y,H*W,0);
		}
		if(S[i][j]!='#')
		{
			P.add_edge(i*W+j,t,1,inf-(i+j));
		}
	}
	int lst=P.flow(s,t).second;
	cout<<inf*cnt-lst-fst<<endl;
}


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

const int MOD=2019;

ll ppow(ll a,ll b){
	a%=MOD;
	ll res=1;
	while(b){
		if(b&1)res=(res*a)%MOD;
		a=(a*a)%MOD;
		b>>=1;
	}
	return res;
}

int main(){
	string s;cin>>s;
	int sum=0;
	map<int,int>mp;
	mp[0]++;
	ll ans=0;
	for(int i=s.size()-1;i>=0;i--){
		sum+=ppow(10,s.size()-i-1)*(int)(s[i]-'0');
		sum%=2019;
		ans+=mp[sum];
		mp[sum]++;
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 3724
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,a,b) for (int i = a; i < (b); ++i)
#define RREP(i,a,b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(),(x).end()
#define YY cout << "Yes" << endl
#define NN cout << "No" << endl
const int INF = 1000000007;
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
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; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); }
int solve(){
  int n;
  cin>>n;
  vector<ll>c(n),s(n),f(n);
  rep(i,n-1)cin>>c[i]>>s[i]>>f[i];
  rep(i,n){
    ll time=0;
    REP(j,i,n-1){
      if(time <= s[j])time=s[j];
      else if((time%f[j]==0))int a;
      else time+=f[j]-(time%f[j]);
      time+=c[j];
    }
    cout<<time<<endl;
  }
  return 0;
}
int main(){
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  // cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
	const string word = "CODEFESTIVAL2016";

	string str;

	cin >> str;

	int count = 0;
	for(int i = 0; i < word.size(); ++i) {
		if(word[i] != str[i])
			count++;	
	}

	cout << count;
	return 0;
}


Measured memory_bytes: 256
def getint():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

num,lim = getint()
l = getint()
sumup = 0
k = True
for i in range(num):
    sumup = sumup + l[i]
    if sumup > lim:
        print(i+1)
        k = False
        break
if k ==True:
    print(num+1)

Measured memory_bytes: 3064
#include <iostream>
#include <numeric>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
using namespace std;
using ll=long long;
const ll MOD=100000007;
const ll LINF=0x1fffffffffffffff;
const int INF=0x3fffffff;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,a,b) for(ll i=b-1;i>=a;i--)
#define range(i) (i).begin(),(i).end()
#define rrange(i) (i).rbegin(),(i).rend()
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline constexpr int intpow(ll a,ll b){
    if(b==0) return 1;
    ll ans=intpow(a,b/2);
    return ans*ans%MOD*(b&1?a:1)%MOD;
}
inline constexpr int modf(ll n){
    ll ans=1;
    rep(i,0,n) {ans=ans*(n-i)%MOD;}
    return ans;
}
inline long double median(vector<ll> v){
    ll size=v.size();
    sort(v.begin(),v.end());
    if (size % 2 == 1) return v[(size - 1) / 2];
    else return v[(size / 2) - 1] ;
}
template<class T> inline bool chmax(T& a,T b) {if(a<b){a=b;return 1;} return 0;}
template<class T> inline bool chmin(T& a,T b) {if(a>b){a=b;return 1;} return 0;}


int main(){
  ll n;
  cin>>n;
  vector<pair<pair<string,ll>,ll>> p(n);
  rep(i,0,n) {
    cin>>p[i].first.first>>p[i].first.second;
  	p[i].second=i;
    p[i].first.second=100-p[i].first.second;
  }
  sort(range(p));
  rep(i,0,n) cout<<p[i].second+1<<endl;
}

Measured memory_bytes: 256
use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;

#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

#[allow(dead_code)]
fn read_as_string() -> String{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}


fn main(){
    let line : Vec<i32> = read_as_vec();
    let (a, p) = (line[0], line[1]);

    println!("{}", (a*3+p)/2);
        
}


Measured memory_bytes: 4352
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <map>
#define REP(i, n) for(int i=0;i<n;++i)
#define rep(i, n) for(int i=0;i<=n;++i)
#define INF 100000000
#define MOD 1000000009
#define ll long long

using namespace std;

typedef pair<int, int> P;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int n;
ll int tmp;
map <ll int, int> mp;

int main(){
    cin >> n;

    REP(i, n){
        cin >> tmp;
        auto itr = mp.find(tmp);
        if(itr != mp.end()){
            ++(itr->second);
        } else {
            mp[tmp] = 1;
        }
    }

    ll int ans;

    for(auto itr=mp.begin(); itr != mp.end(); ++itr){
        if(itr->second < itr->first){
            ans += itr->second;
        } else {
            ans += (itr->second - itr->first);
        }
    }

    cout << ans << endl;

	return 0;
}

Measured memory_bytes: 6272
#include <stdio.h>

int main() {
    int a, b;
    char op;
    while (scanf("%d %c %d", &a, &op, &b) == 3) {
        if (op == '+') {
            printf("%d\n", a + b);
        } else if (op == '-') {
            printf("%d\n", a - b);
        } else if (op == '*') {
            printf("%d\n", a * b);
        } else if (op == '/') {
            printf("%d\n", a / b);
        } else if (op = '?') {
            break;
        }
    }
    return 0;
}

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

int main() {
	long long n;
	cin >> n;
	/*vector<long long> da(0);
	vector<int> li(0);
	for (long long i = 2;i < 100001;i++) {
		if (n%i == 0) {
			da.push_back(i);
			int x = 0;
			while (n / i == 0) {
				x++;
				n /= i;
			}
			li.push_back(x);
		}
	}*/
	long long ans = 0;
	for (int i = 1;i < 1000001;i++) {
		if (n / i < i)
			break;
		if (n % i == 0 && n / i - 1 > i)
			ans += n / i - 1;
	}
	cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define em3 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define EPS 1e-7
long long mod = 1e9 +7;
typedef long long ll;
typedef long double ld;
const double pi = acos(-1);
typedef complex <double> point;
typedef tuple<int, int, int> line;
typedef vector<point> polygon;


int main()
{

    em3
    int n , h[100005] , ans[100005] , x;
    cin >> n >> x;
    for(int i = 0 ; i < n ; i++)
        cin >> h[i];
    ans[0] = 0, ans[1] = abs(h[1] - h[0]);
    for(int i = 2 ; i < n ; i++)
    {
        ans[i] = 1e9;
        for(int j = i - 1 ; j >= max(0 , i - x) ; j--)
            ans[i] = min(ans[i] , ans[j] + (abs(h[i] - h[j])));
    }
    cout << ans[n - 1] << endl;

    return 0;
}



Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 200005
using namespace std;

int n, k, s, c[N];
int main()
{
	int i, t;
	cin >> n >> k;
	for (i = 0; i < n; i++) {
		scanf("%d", &t);
		if (++c[t] == 1) k--;
	}
	sort(c, c + n + 1);
	for (i = 0; i < n && k < 0; i++) {
		if (!c[i]) continue;
		s += c[i], k++;
	}
    cout << s << endl;
    return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
int n,x,v[105];
void GG(){
	puts("Impossible");
	exit(0);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&x);
		v[x]++;
	}
	int l=n;
	while (!v[l])l--;
	int m=l+1>>1;
	if (l==1&&n>2)GG();
	if ((l&1)&&(v[m]^2))GG();
	if (!(l&1)&&(v[m]^1)&&n>2)GG();
	for (int i=m+1;i<=l;i++)
		if (v[i]<2)GG();
	for (int i=1;i<m;i++)
		if (v[i])GG();
	puts("Possible");	
}

Measured memory_bytes: 256
a,b = gets.chomp.split.map(&:to_i)
x = 0
y = 0
xs = []
ys = []
ans = []
while (y * 0.1).floor <= b
  if (y * 0.1).floor == b
    ys << y
  end
  y += 1
end
while (x * 0.08).floor <= a
  if (x * 0.08).floor == a
    xs << x
  end
  x += 1
end

xs.each do |x|
  ys.each do |y|
    if x == y
      ans << x
    end
  end
end
if ans.empty?
  ans << -1
end
puts ans.min

Measured memory_bytes: 3836
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
    std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
    std.random, std.typecons, std.container;

ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);

void main()
{
    auto n = lread();
    long ok = -1, ng = n;
    writeln(0);
    stdout.flush();
    auto first = sread();
    if (first == "Vacant")
        return;

    long p = (first == "Male" ? 0 : 1);
    while (abs(ok - ng) > 1)
    {
        auto mid = (ok + ng) / 2;
        mid.writeln();
        stdout.flush();
        auto s = sread();

        bool check(long i, string s)
        {
            if ((i + p) % 2)
                return (s == "Female");
            else
                return (s == "Male");
        }

        if (s == "Vacant")
            return;

        if (check(mid, s))
            ok = mid;
        else
            ng = mid;

    }
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}


Measured memory_bytes: 3808
#include<stdio.h>
int main(){
    int n, k, c = 0, r = 0;
    int a = 0, l = 0;
    scanf("%d%d",&n,&k);
    char s[n + 1];
    scanf("%s",s);
    if(s[0] == '0')
        c = 1;
    for(l = 0; l < n; l++){
        while(c < k + 1){
            if(s[r] == '1' && s[r + 1] == '0')
                c++;
            if(r == n)
                goto END;
            r++;
        }
        if(a < r - l)
        a = r - l;
        if(s[l] == '0' && s[l + 1] == '1')
            c--;
    }
    END : ;
    if(a < r - l)
        a = r - l;
    printf("%d\n",a);
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<bitset>
#include<vector>
#include<algorithm>
#include<cassert>
#include<cmath>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)

using namespace std;
int n;
string pip[5];
int state;
int ans;
int maxim;

void create_state()
{
  state = 1;
  REP(i,1,n)
    {
      rep(j,n)
	{
	  if(i+j >= n)break;
	  rep(k,5)
	    {
	      if(pip[k][j] == 'X' && pip[k][i+j] == 'X')
		{
		  state |= (1<<i);
		  goto Next;
		}
	    }
	}
    Next:;
    }
}

void create_maxim()
{
  maxim = 0;
  rep(i,5)rep(j,n)if(pip[i][j] == 'X')maxim = max(maxim,j);
}

void rec(int now_state,int cnt,int value)
{
  if(value>=ans)return;
  if(cnt >= 10)
    {
      ans = min(ans,value);
      return;
    }

  rep(i,n+1)
    {
      if((now_state >> i) & 1)continue;
      int next_state = (now_state >> i) | state;

      rec(next_state,cnt+1,value+i);
    }

}

int main()
{
  while(cin >> n,n)
    {
      ans = inf;
      rep(i,5)cin >> pip[i];
      create_state();
      create_maxim();
      rec(state,1,maxim);
      cout << ans+1 << endl;
    }
  return 0;
}

Measured memory_bytes: 1204
import sys

sys.setrecursionlimit(10 ** 6)
_ = int(input())
A = list(map(int, input().split()))


def solve(i, s, k, sign):
    if i >= len(A):
        return k

    if sign * (s + A[i]) > 0:
        return solve(i + 1, s + A[i], k, sign * -1)
    else:
        return solve(i + 1, sign, k + abs(s + A[i]) + 1, sign * -1)


print(min(solve(0, 0, 0, 1), solve(0, 0, 0, -1)))


Measured memory_bytes: 97700
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vpi = vector<pii>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpl = vector<pll>;
using vs = vector<string>;

int main()
{
    string s, t;
    cin >> s >> t;
    bool ans = true;
    for(int i=0; i<s.size(); i++)
    {
        if(s.at(i) != t.at(i)) ans = false;
    }
    if(ans && s.size() + 1 == t.size()) cout << "Yes\n";
    else cout << "No\n";
    return 0;
}


Measured memory_bytes: 3596
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner std = new Scanner(System.in);
        int[] ns = new int[4];
        for (int i = 0; i < ns.length; i++) {
            int n = std.nextInt();
            ns[i] = n;
        }

        Arrays.sort(ns);
        boolean requiredCondition = ns[0] == 1 && ns[1] == 4 && ns[2] == 7 && ns[3] == 9;
        if (requiredCondition) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

Measured memory_bytes: 23380
#include <bits/stdc++.h>
#define int long long

using namespace std;
using LL = long long;
using P = pair<int, int>;

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

#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

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

const int INF = (int)1e9;
const LL INFL = (LL)1e18;
const int MOD = 1e9 + 7;

class BipartiteGraph {
    private:
        vector<vector<int>> graph;
        int V; // 頂点数
        bool Dfs(int v, int color);

    public:
        vector<int> color; // 頂点iの色(1 or -1)
        BipartiteGraph(int n);
        void AddEdge(int f, int t);
        bool Run(); // 2部グラフか否か
};

BipartiteGraph::BipartiteGraph(int n):
    graph(vector<vector<int>>(n)),
    V(n),
    color(vector<int>(n))
{}

void BipartiteGraph::AddEdge(int f, int t){
    graph[f].push_back(t);
}

bool BipartiteGraph::Dfs(int v, int c){
    color[v] = c;
    for(auto x: graph[v]){
        if(color[x] == c) return false;
        if(color[x] == 0 && !Dfs(x, -c)) return false;
    }

    return true;
}

bool BipartiteGraph::Run(){
    for(int i = 0; i < V; i++){
        if(color[i] == 0){
            if(!Dfs(i, 1)){
                return false;
            }
        }
    }
    return true;
}

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n, m;
    cin >> n >> m;
    
    BipartiteGraph bg(n);
    REP(i, m)
    {
        int a, b; 
        cin >> a >> b;
        a--, b--;
        bg.AddEdge(a, b);
        bg.AddEdge(b, a);
    }

    if(bg.Run())
    {
        int b = 0, w = 0;
        REP(i, n) (bg.color[i] == 1 ? b : w)++;
        cout << b*w - m << endl;
    }
    else 
    {
        cout << n*(n-1)/2 - m << endl;
    }
}

Measured memory_bytes: 8704
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {

	public static final int BIG_NUM = 2000000000;
	public static final int MOD = 1000000007;
	public static final long HUGE_NUM = 99999999999999999L;
	public static final double EPS = 0.000000001;

	public static int H,W;

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {

		//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Scanner scanner = new Scanner(System.in);
		int SIZE = 155;

		int index_table[][] = new int[SIZE+1][SIZE+1];

		PriorityQueue<Info> Q = new PriorityQueue<Info>();
		ArrayList<Info> list = new ArrayList<Info>();

		for(int h = 1; h <= SIZE-1; h++){
			for(int w = h+1; w <= SIZE; w++){

				Q.add(new Info(h,w));
			}
		}

		int index = 0;

		while(!Q.isEmpty()){

			index_table[Q.peek().H][Q.peek().W] = index++;
			list.add(Q.peek());
			Q.poll();
		}

		StringBuilder ans = new StringBuilder();

		while(true){

			H = scanner.nextInt();
			W = scanner.nextInt();

			if(H == 0 && W == 0)break;

			int tmp_index = index_table[H][W]+1;

			ans.append(Integer.toString(list.get(tmp_index).H)).append(" ").
				append(Integer.toString(list.get(tmp_index).W)).append("\n");
		}

		System.out.print(ans.toString());
	}
}

class Info implements Comparable<Info>{

	Info(){

		H = W = 0;
		value = 0;
	}

	Info(int arg_H,int arg_W){
		H = arg_H;
		W = arg_W;
		value = H*H+W*W;
	}

	 public int compareTo(Info arg){
        if(this.value < arg.value)return -1;
        if(this.value > arg.value)return 1;
        if(this.H < arg.H)return -1;
        if(this.H > arg.H) return 1;
        return 0;
    }

	public int H;
	public int W;
	public int value;
}


Measured memory_bytes: 28852
#include<bits/stdc++.h>
#include<atcoder/segtree>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a)  (a).begin(),(a).end()
ll ans=0;
int op(int a, int b){
    return max(a, b);
}
int e(){
    return -1;
}
int to;
bool f(int v){
    return v < to;
}
void Main(){
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    For(i,0,n) cin >> a[i];
    segtree<int, op, e> seg(a);
    while(q--){
        int t, x, y;
        cin >> t >> x >> y;
        x--;
        if(t == 1){
            seg.set(x, y);
        }
        else if(t == 2){
            cout << seg.prod(x, y) << endl;
        }
        else if(t == 3){
            to = y;
            cout << seg.max_right<f>(x) + 1 << endl;
        }
    }
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}

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

#define int long long
//#define uint __int128_t

#define pb push_back
#define fi first
#define se second
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
#define rrep(i,s,n) for(int i = (n)-1;i>=(s);i--)
#define all(v) (v).begin(),(v).end()
#define chmin(a,b) a=min((a),(b))
#define chmax(a,b) a=max((a),(b))
#define low(a,b) lower_bound(all(a),(b))-(a).begin()
#define upp(a,b) upper_bound(all(a),(b))-(a).begin()
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<vint> vvint;
typedef vector<pint>vpint;
typedef pair<pint,int> P1;
typedef pair<int,pint> P2;
typedef pair<pint,pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD=1000000007;
const ll INF=1e18;

int N;
int h[100010];

signed main() {
    IOS();
    //(1ll<<N)シフト演算のオーバーフローに気をつける
    //MOD同士の引き算に注意
    cin>>N;
    rep(i,0,N)cin>>h[i];
    int ans=0,tmp=0;
    rep(i,0,N){
        if(h[i]<=tmp){
            tmp=h[i];
            continue;
        }
        ans+=h[i]-tmp;
        tmp=h[i];
    }
    cout<<ans<<endl;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#define swap(type, x, y)  do{ type t = x;  x = y;  y = t; }while(0)

void quick(int array[], int left, int right){
  int pl = left, pr = right, x = array[(left + right) / 2];
  do{
    while(array[pl] < x)  pl++;
    while(array[pr] > x)  pr--;
    if(pl <= pr){
      swap(int, array[pl], array[pr]);
      pl++, pr--;
    }
  }while(pl <= pr);
  if(pl < right)  quick(array, pl, right);
  if(pr > left)   quick(array, left, pr);
}

int main(){
  int n;  scanf("%d", &n);
  int x[200005], xcopy[200005];
  for(int i=0; i<n; i++){
    scanf("%d", x+i);
    xcopy[i] = x[i];
  }
  quick(x, 0, n-1);
  int medianl = x[n/2-1],  medianr = x[n/2];
  
  for(int i=0; i<n; i++){
    if(xcopy[i] <= medianl)  printf("%d\n", medianr);
    else                     printf("%d\n", medianl);
  }

  //  printf("%ls\n", x);

  return 0;
}
  


Measured memory_bytes: 3712
#include <iostream>
#include <string>
#include <algorithm> 
#include <cstdlib>
#include <map>
#include <queue>
#include <vector>
#include <utility>
#include <climits>
#include <numeric>
#define ll long long
#define INF INT_MAX 
using namespace std;
int main(){
	int a[3];
	cin >> a[0] >> a[1] >> a[2];
	int count=0;
	sort(a,a+3);
	reverse(a,a+3);
	if(a[0]==a[2]){
		cout << 0 << endl;
		return 0;
	}
	if(a[0]!=a[1]){
		while(1){
			a[1]++;
			a[2]++;
			count++;
			if(a[0]==a[1]){
				while(1){
					if(a[2]==a[0]){
						cout << count << endl;
						return 0;
					}else if(a[2]>a[0]){
						cout << count + 1 << endl;
						return 0;
					}
					a[2]+=2;
					count++;
				}
			}
		}
	}else{
		while(1){
			if(a[2]==a[0]){
				cout << count << endl;
				return 0;
			}else if(a[2]>a[0]){
				cout << count+1 << endl;
				return 0;
			}
			a[2]+=2;
			count++;
		}
	}
}

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

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 200005;
int N;
int Q[MAX_N], A[MAX_N], B[MAX_N];

map<int, int> Counter;
int lCnt = 0, rCnt = 0;
int lSum = 0, rSum = 0;

set<int> S;

int sumB = 0;
int sumA = 0;

int midS = 0;
int queryCnt = 0;

signed main()
{
  cin >> N;
  REP(n,N) {
    cin >> Q[n];
    if(Q[n] == 1) {
      queryCnt++;
      cin >> A[n] >> B[n];
      sumB += B[n];
      
      if(S.size()==0) {
        midS = A[n];
      }
      
      S.insert(A[n]);
      Counter[A[n]]++;
      
      if(A[n] < midS) {
        lCnt++;
        lSum += midS - A[n];
      } else if(A[n] > midS) {
        rCnt++;
        rSum += A[n] - midS;
      }

      if(lCnt < (queryCnt+1)/2 && (queryCnt+1)/2 <= lCnt + Counter[midS]) {
        cerr << n << ": OK " << midS << " " << lCnt << " " << rCnt << " " << lSum << " " << rSum << endl;
      } else {
        if(lCnt > rCnt) { //左にずらす
          auto it = S.lower_bound(midS);
          if(it != S.begin()) {
            it--;
            int newS = *it;

            lCnt -= Counter[newS];
            rCnt += Counter[midS];

            cerr << lSum << " " << rSum << endl;
            lSum -= (midS - newS) * (lCnt + Counter[newS]);
            rSum += (midS - newS) * rCnt;
            cerr << lSum << " " << rSum << endl;

            midS = newS;
          } else {
            midS = *it;
          }
          
          cerr << n << ": 左 " << midS << " " << lCnt << " " << rCnt << " " << lSum << " " << rSum << endl;
        } else { //右にずらす
          auto it = S.lower_bound(midS);
          it++;
          if(it == S.end()) {
            it--;
            midS = *it;
          } else {
            int newS = *it;
            lCnt += Counter[midS];
            rCnt -= Counter[newS];
            // cerr << lSum << " " << rSum << endl;
            lSum += (newS - midS) * lCnt;
            rSum -= (newS - midS) * (rCnt + Counter[newS]);
            // cerr << lSum << " " << rSum << endl;
            midS = newS;
          }

          cerr << n << ": 右 " << midS << " " << lCnt << " " << rCnt << " " << lSum << " " << rSum << endl;
        }
      }
    } else {
      cout << midS << " " << lSum + rSum + sumB << endl;
    }
  }



  return 0;
}

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

ll solve(string s){
    vector<ll> nums;
    int n = s.size();
    ll cnt = 0;
    char prev = s[0];
    for(int i = 1; i < n; ++i){
        if(s[i] == prev){
            cnt++;
        }else if(cnt > 0){
            nums.push_back(cnt+1);
            cnt = 0;
        }
        prev = s[i];
    }
    if(cnt > 0){
        nums.push_back(cnt+1);
        cnt = 0;
    }

    ll ans = 0LL;
    for(ll c : nums){
        ans += (c&1 ? c-1 : c)/2LL;
    }
    return ans;
}

int main(){
    string s;
    ll k;
    cin >> s >> k;
    int n = s.size();
    if(n == 1){
        cout << (k&1LL ? k-1LL : k)/2LL << endl;
        return 0;
    }
    ll tmp = 1LL;
    while(s[0] == s[tmp]){
        tmp++;
        if(tmp == n){
            cout << ((k*n)&1LL ? k*n-1LL : k*n)/2LL << endl;
            return 0;
        }
    }

    if(s[0] != s[n-1]){
        cout << solve(s)*k << endl;
        return 0;
    }
    else{
        string str = s;
        int t = 1;
        char x = str[0];
        while(x == str[1]){
            t++;
            str += str[0];
            str = str.substr(1);
        }
        str += str[0];
        str = str.substr(1);


        ll ans = solve(str)*(--k) +solve(s.substr(0,t)) + solve(s.substr(t));


        cout << ans << endl;
    }
}


Measured memory_bytes: 256
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::io::{stdin, stdout, Read, Write};
use std::str::FromStr;

#[derive(Default)]
struct Scanner<R: Read> {
    reader: R,
}

impl<R: Read> Scanner<R> {
    fn new(reader: R) -> Scanner<R> {
        Scanner { reader }
    }

    fn scan<T: FromStr>(&mut self) -> Option<T> {
        let s = self
            .reader
            .by_ref()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        s.parse::<T>().ok()
    }

    fn next<T: FromStr>(&mut self) -> T {
        if let Some(s) = self.scan() {
            s
        } else {
            std::process::exit(1);
        }
    }
}

struct Node {
    id: usize,
    parent: usize,
    left: usize,
    right: usize,
}

fn dfs_preorder<T: Write>(i: usize, node: &[Node], out: &mut T) {
    write!(out, " {}", i).ok();
    if node[i].left != i {
        dfs_preorder(node[i].left, node, out);
    }
    if node[i].right != i {
        dfs_preorder(node[i].right, node, out);
    }
}

fn dfs_inorder<T: Write>(i: usize, node: &[Node], out: &mut T) {
    if node[i].left != i {
        dfs_inorder(node[i].left, node, out);
    }
    write!(out, " {}", i).ok();
    if node[i].right != i {
        dfs_inorder(node[i].right, node, out);
    }
}

fn dfs_postorder<T: Write>(i: usize, node: &[Node], out: &mut T) {
    if node[i].left != i {
        dfs_postorder(node[i].left, node, out);
    }
    if node[i].right != i {
        dfs_postorder(node[i].right, node, out);
    }
    write!(out, " {}", i).ok();
}


fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin);
    let out = stdout();
    let mut out = out.lock();

    let n = sc.next();
    let mut node = Vec::<Node>::new();
    for i in 0..n {
        node.push(Node {
            id: i,
            parent: i,
            left: i,
            right: i,
        })
    }

    for _ in 0..n {
        let i: usize = sc.next();
        let l: i32 = sc.next();
        let r: i32 = sc.next();
        if l != -1 {
            let l = l as usize;
            node[i].left = l;
            node[l].parent = i;
        }
        if r != -1 {
            let r = r as usize;
            node[i].right = r;
            node[r].parent = i;
        }
    }

    let root = node.iter().find(|x| x.id == x.parent).unwrap().id;
    writeln!(out, "Preorder").ok();
    dfs_preorder(root, &node, &mut out);
    writeln!(out).ok();
    writeln!(out, "Inorder").ok();
    dfs_inorder(root, &node, &mut out);
    writeln!(out).ok();    writeln!(out, "Postorder").ok();
    dfs_postorder(root, &node, &mut out);
    writeln!(out).ok();
}



Measured memory_bytes: 5032
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<map>
#include<numeric>
#include<set>
#include<string>
#include<utility>
#include<vector>

#define rep(i, a, n)  for(int i=a; i<n; i++)
#define per(i, a, n)  for(int i=n-1; i>=a; i--)
#define all(x)  (x).begin(), (x).end()
#define MAX 1000000007
using namespace std;
typedef long long ll;

int main(){
  float t, x; cin >> t >> x;
  cout << t/x << endl;
}

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

int main(void){
    int N;
    cin >> N;
    int d[N];
    for(int i = 0; i < N; i++){
        cin >> d[i];
    }

    sort(d, d+N);
    int tmp = d[0],count = 1;
    for(int i = 0; i < N; i++){
        if(tmp != d[i]){
            count++;
            tmp = d[i];
        }
    }

    cout << count <<endl;
    return 0;
}

Measured memory_bytes: 256
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  q,h,s,d=LI()
  n=I()

  q*=4
  h*=4
  s*=4
  d*=4
  n*=4

  l=[]
  l.append([q,1,q/1])
  l.append([h,2,h/2])
  l.append([s,4,s/4])
  l.append([d,8,d/8])

  l=sorted(l,key=lambda x:x[2])

  ans=0
  for x in l:
    if n%x[1]==0:
      ans+=(n//x[1])*x[0]
      return ans//4
    else:
      ans+=(n//x[1])*x[0]
      n%=x[1]

# main()
print(main())


Measured memory_bytes: 6480
n=int(input())

ch=[0]*10001

for i in range(1,100):
  for j in range(1,100):
    for k in range(1,100):
      x=i**2+j**2+k**2+i*j+j*k+k*i
      if x<=10000:
        ch[x]+=1
for i in range(n):
  print(ch[i+1])


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

template <typename T, typename U>
U sum(T first, T last, U acc){
	while(first != last){
		acc += *first++;
	}
	return acc;
}

int main(){
	int n, m;
	cin >> n >> m;
	int num[n];
	for(int i = 0; i < n; i++) num[i] = 1;
	bool in[n]{};
	in[0] = true;
	while(m--){
		int x, y;
		cin >> x >> y;
		x--;
		y--;
		num[x]--;
		num[y]++;
		if(in[x]) in[y] = true;
		if(num[x] == 0) in[x] = false;
	}
	cout << sum(in, in + n, 0) << endl;
}

Measured memory_bytes: 768
import heapq
import itertools

def dijkstra(graph, node, start):
    dist = [float("inf") for _ in range(node)]

    dist[start] = 0
    q = []
    heapq.heappush(q, (0, start))

    while q:
        cost, cur_node = heapq.heappop(q)

        if dist[cur_node] < cost:
            continue

        for nex_cost, nex_node in graph[cur_node]:
            dist_cand = dist[cur_node] + nex_cost
            if dist_cand < dist[nex_node]:
                dist[nex_node] = dist_cand
                heapq.heappush(q, (dist[nex_node], nex_node))

    return dist

n, m, R = map(int, input().split())
r = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for i in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append((c, b))
    adj[b].append((c, a))
shortest = []
for i in range(n):
    shortest.append(dijkstra(adj, n, i))
ans = float("INF")
for i in itertools.permutations(r):
    s = 0
    for j in range(R - 1):
        s += shortest[i[j] - 1][i[j + 1] - 1]
    ans = min(ans, s)
print(ans)

Measured memory_bytes: 8364
x = int(input())
if x == 2:
    print(2)
    exit()
elif x == 3:
    print(3)
    exit()
while True:
    for i in range(2,int(x**0.5)+1):
        if x % i == 0:
            c = False
            break
        c = True
    if c == True:
        print(x)
        break
    elif c == False:
        x += 1
        
    


Measured memory_bytes: 38512
n=int(input())
cards=[[False for i in range(13)] for y in range(4)]
pattern = ['S','H','C','D']

for i in range(n):
    ch,rank = map(str,input().split())
    rank = int(rank)
    cards[pattern.index(ch)][rank-1] = True

for i in range(4):
    for j in range(13):
        if cards[i][j] == False:
            print(pattern[i],j +1)

        
        
        

            


Measured memory_bytes: 5600
#include<stdio.h>

int main(){
	
	int A,B,coin=0;
	scanf("%d",&A);
	scanf("%d",&B);
	
	if(A+(A-1)>A+B&&A+(A-1)>B+(B-1)){
	
	coin=2*A-1;
	}else if(2*B-1>2*A-1&&2*B-1>A+B){
		coin=2*B-1;
		
		
	}else {
		coin=A+B;
		
	}
	
	printf("\n%d\n\n",coin);
	
	return 0;
}

Measured memory_bytes: 256
n = int(input())
h = list(map(int,input().split()))
ans = [0] * n
ans[1] = abs(h[1] - h[0])
for i in range(2,n):
  ans[i] = min(abs(h[i] - h[i-1]) + ans[i-1], abs(h[i] - h[i-2]) + ans[i-2])
print(ans[-1])

Measured memory_bytes: 13980
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
typedef long long ll;
const int INF = 1e9+1;
const int MOD = 2e9 + 9;
const ll LINF = 1e18;
using namespace std;

int main(){
	int n;
	int sum=0;
	cin >> n;
	vector<int> a(n+2);
	a.at(0) = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a.at(i);
		sum += abs(a.at(i) - a.at(i - 1));
	}
	sum += abs(a.at(n));
	//cout << sum << endl;
	for (int i = 1; i <= n; i++) {
		int nsum = sum;
		nsum -= abs(a.at(i) - a.at(i - 1));
		nsum -= abs(a.at(i + 1) - a.at(i));
		nsum += abs(a.at(i + 1) - a.at(i - 1));
		cout << nsum << endl;
	}
	return 0;
}


Measured memory_bytes: 3784
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    A,B,N = map(int,input().split())

    if B <= N:
        res = A * (B - 1) // B
    else:
        res = A * N // B

    print(res)



main()


Measured memory_bytes: 9108
#include <bits/stdc++.h>
 
#define int long long 

using namespace std;

int N;
int seq[3000];
int xminusy[2][3001][3001];

int getbestsc(bool player,int cursbe,int cursaf)
{
	if(cursaf==cursbe)
	{
		return (player ? -1 : 1)*seq[cursaf];
	}
	if(xminusy[player][cursbe][cursaf]!=-1)
	{
		return xminusy[player][cursbe][cursaf];
	}
	int gscore;
	if(player)
	{
		gscore = min(-seq[cursbe]+getbestsc(0,cursbe+1,cursaf),
					 -seq[cursaf]+getbestsc(0,cursbe,cursaf-1));
	}
	else
	{
		gscore = max(seq[cursbe]+getbestsc(1,cursbe+1,cursaf),
					 seq[cursaf]+getbestsc(1,cursbe,cursaf-1));
	}
	xminusy[player][cursbe][cursaf]= gscore;
	//cout<<"player "<<player<<" seq from "<<cursbe<<" to "<<cursaf<<" gives score : "<<gscore<<endl;
	return gscore;
	
}

signed main()
{
	ios::sync_with_stdio(false);
	cin>>N;
	for(int i = 0 ; i < N ; i++)
	{
		cin>>seq[i];
		for(int j = 0 ; j <= N ; j ++ )
		{
			xminusy[0][i][j]=-1;
			xminusy[1][i][j]=-1;
		}
	}
	for(int j = 0 ; j <= N ; j ++ )
	{
		xminusy[0][N][j]=-1;
		xminusy[1][N][j]=-1;
	}
	cout<<getbestsc(0,0,N-1);
}

Measured memory_bytes: 141184
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T>
inline vector<T> make_v(const T &initvalue, size_t sz) {
    return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initvalue, args...))>(sz, make_v<T>(initvalue, args...));
}

constexpr int INF = 0x3f3f3f3f;
constexpr int64_t LINF = 0x3f3f3f3f3f3f3f3fLL;

using pii = pair<int64_t, int64_t>;
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<>>;

constexpr int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

inline bool inside(int y, int x, int H, int W) {
    return (y >= 0 && x >= 0 && y < H && x < W);
}
// }}} End

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    string N; cin >> N;

    int digitSum = 0;
    for (size_t i = N.length() - 1; i > 0; --i) {
        if (N[i] != '9') {
            N[i - 1]--;
        }
        digitSum += 9;
    }

    cout << digitSum + (N[0] - '0') << "\n";

    return 0;
}


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

int csum[100];
int main(void){
  int r, c, t=0;
  cin >> r >> c;
  
  for (int i=0; i<r; i++){
    int rsum=0;
    for (int j=0; j<c; j++){
      int t;
      cin >> t;
      cout << t << " ";
      rsum += t;
      csum[j] += t;
    }
    cout << rsum << endl;
  }
  for (int j=0; j<c; j++){
    cout << csum[j] << " ";
    t += csum[j];
  }
  cout << t << endl;
    
  return 0;
}



Measured memory_bytes: 3108
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
ll M=1e9+7;
int main()
{
	ll n,ans=0,x=0;
	cin>>n;
	vector<ll> vec(n+1,0);
	for(ll i=1;i<=n;i++){
		cin>>vec[i];
		vec[i]=vec[i]%M;
		ans+=(x*vec[i]);
		ans=(ans%M);
		x+=vec[i];
		x=(x%M);
	}
	cout<<ans<<endl;
	
}

Measured memory_bytes: 4824
# 2019/08/09
#だめなやつ

from collections import Counter
s=list(input())
n=len(s)
S=set(s)
"""
if len(s)==len(S):
    print(n//2)
    exit()"""
if s.count(s[0])==len(s):
    print(0)
    exit()


res=float('inf')
for e in S:
    r=0
    l=0
    cnt=0
    
    while l<=r<n:
        if s[r]==e:
            if l==0:
                cnt=max(r-l,cnt)
            else:
                cnt=max(r-l-1,cnt)
            l=r
        r+=1
    if n==r:cnt=max(r-l-1,cnt)
    if cnt==0:continue
    res=min(res,cnt)
if res==float('inf'):
    print(0)
else:print(res)

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

#define int long long
#define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)x.size())
template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  }*/

int n, m;
vi es[601];

signed main(){
  cin >> n >> m;
  rep(i, 0, m){
    int s, t;
    cin >> s >> t;
    es[s].emplace_back(t);
  }
  
  double ans = inf;
  rep(k, 0, n){
    vector<double> dp(n+1);
    dp[n] = 0;
    for(int i= n-1; i > 0; i--){
      double sum = 0.0;
      double ma = 0.0;
      int num = sz(es[i]);

      for(auto to : es[i]){
        sum += dp[to];
        chmax(ma, dp[to]);
      }

      if(i == k and num >= 2){
        num--;
        sum -= ma;
      }

      dp[i] = sum/num + 1;
    }

    chmin(ans, dp[1]);
  }

  printf("%.9f\n", ans);

  return 0;
}


Measured memory_bytes: 2560
#include<stdio.h>
int main()
{

    int a,b,lol=0,lol1=0;
    scanf("%d%d",&a,&b);
    if(a>=b){
        lol=a/b;
        if(lol<2){printf("Yay!");}
        else{printf(":(");}
    }
    else{
        lol1=b/a;
        if(lol1<2){
            printf("Yay!");
        }
        else {
            printf(":(");
        }

}
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
int main(){
  cin.tie(nullptr);ios::sync_with_stdio(false);
  int N;string s;cin >> N >> s;
  int i,ans = 0,sum = count(s.begin(),s.end(),'R');
  for(i=sum;i<N;i++) ans += s[i]=='R';
  cout << ans << "\n";
}

Measured memory_bytes: 3732
n,m,*t=map(int,open(0).read().split())
e=[[]for _ in'_'*n]
for a,b in zip(*[iter(t)]*2):
  e[a-1]+=b-1,
  e[b-1]+=a-1,
s=[0]
f=s+[-1]*~-n
while s:
  v=s.pop()
  p=f[v]^1
  for w in e[v]:
    if-1<f[w]:
      if f[w]!=p:
        print(n*~-n//2-m)
        exit()
    else:
      f[w]=p
      s+=w,
r=sum(f)
print(r*(n-r)-m)

Measured memory_bytes: 82800