akhauriyash/Code-Regression huggingface dataset:
#include<stdio.h>
int main(){
  int n,i,j,r[25],t[25],b[25],a,s;
  while(scanf("%d",&n),n){
    for(i=0;i<n;i++){
      r[i]=i;
      scanf("%d %d",&t[i],&b[i]);
    }
    for(i=0;i<n;i++){
      for(j=1;i+j<n;j++){
	if(b[r[j]]<b[r[j-1]]){
	  a=r[j];
	  r[j]=r[j-1];
	  r[j-1]=a;
	}
      }
    }
    s=0;
    for(i=0;i<n;i++){
      s+=t[r[i]];
      if(s>b[r[i]])break;
    }
    if(n-i)printf("No\n");
    else   printf("Yes\n");
  }
  return 0;
}

Measured memory_bytes: 584
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>

#define MAX_N 8

using namespace std;

int X[MAX_N];
int Y[MAX_N];
int A[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i];
    A[i] = i;
  }
  double d = 0;
  int cnt = 0;
  do {
    double _d = 0;
    for (int i = 0; i < N - 1; ++i) {
      _d += sqrt((X[A[i]] - X[A[i + 1]]) * (X[A[i]] - X[A[i + 1]]) +
                 (Y[A[i]] - Y[A[i + 1]]) * (Y[A[i]] - Y[A[i + 1]]));
    }
    d += _d;
    ++cnt;
  } while (next_permutation(A, A + N));
  printf("%f\n", d / cnt);
}

Measured memory_bytes: 3232
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main(void){
    int n,k;
    cin >> n >> k;
    int candle[n];
    for(int i=0; i<n; i++){
        cin >> candle[i];
    }
    int base=n;
    for(int i=0; i<n; i++){
        if(candle[i]>=0){
            base=i;
            break;
        }
    }
    int sum[n+1]={0},rev[n+1]={0};
    //sumは1.revも1から
    int summax=0,revmax=0;
    for(int i=0; i+base<n; i++){
        sum[i+1]=candle[i+base];
        summax=i+1;
    }
    for(int i=0; base-(i+1)>=0; i++){
        rev[i+1]=candle[base-(i+1)];
        revmax=i+1;
    }
    long long int ans=100000000000000000,temp;
    if(base!=n&&base+k-1<n){
        if(candle[base+k-1]==0){
        cout << "0" <<endl;
        return 0;
        }
    }
    for(int i=0; i<=k; i++){
        temp=min(sum[i]*2+rev[k-i]*(-1),sum[i]+rev[k-i]*(-2));
        if(i>summax||k-i>revmax)continue;
        ans=min(ans,temp);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1408
#include<bits/stdc++.h>
using namespace std;
const int N=309;
int n,m,k;
char a[N][N];
bool hav[N];
int ans[N][N]; 
void solve(vector<int>h,int x,int &g){
  int bg=1,ed=1;
  while(ed<=m&&a[x][ed]=='.')ed++;
  while(bg<=m){
    //cout<<x<<" "<<bg<<" "<<ed<<endl;
    while(ed+1<=m&&a[x][ed+1]=='.')ed++;
      g++;
      for(auto i:h)
        for(int j=bg;j<=ed;j++)ans[i][j]=g;
    bg=ed+1;ed=bg;
  } 
}                                  
int main(){
//  freopen("out.txt","w",stdout);
  cin>>n>>m>>k;
  for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)cin>>a[i][j],hav[i]|=(a[i][j]=='#');
  int cnt=0;
  vector<int>h;
  int i=1;
  while(i<=n&&!hav[i])h.push_back(i),i++;
  while(i<=n){
    h.push_back(i),i++;
    int x=i-1;
    while(i<=n&&!hav[i])h.push_back(i),i++;
    solve(h,x,cnt);
    h.clear();
  }
  for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++)cout<<ans[i][j]<<" ";
    cout<<endl;
  }
  return 0;
} 

Measured memory_bytes: 1280
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define MOD 1000000007
#define INF 1<<30
#define LINF (ll)1<<62
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main(){
    ll n; cin >> n;
    vl a(n);
    ll mcnt = 0;
    ll pcnt = 0;
    rep(i,n){
        cin >> a[i];
        if(a[i] <= 0) mcnt++;
        if(a[i] >= 0) pcnt++;
    }
    sort(all(a));
    vpl ans;
    if(mcnt == n){
        for(ll i=n-2; i>=0; i--){
            ans.emplace_back(a[n-1],a[i]);
            a[n-1] -= a[i];
        }
    }else if(pcnt == n){
        REP(i,1,n-1){
            ans.emplace_back(a[0],a[i]);
            a[0] -= a[i];
        }
        ans.emplace_back(a[n-1],a[0]);
        a[n-1] -= a[0];
    }else{
        ll index;
        rep(i,n){
            if(a[i] >= 0){
                index = i;
                break;
            }
        }
        if(index == n-1){
            rep(i,n-1){
                ans.emplace_back(a[n-1],a[i]);
                a[n-1] -= a[i];
            }
        }else{
            REP(i,index+1,n-1){
                ans.emplace_back(a[0],a[i]);
                a[0] -= a[i];
            }
            if(a[index] >= 0){
                ans.emplace_back(a[0],a[index]);
                a[0] -= a[index];
                rep(i,index){
                    ans.emplace_back(a[n-1],a[i]);
                    a[n-1] -= a[i];
                }
            }else{
                rep(i,index+1){
                ans.emplace_back(a[n-1],a[i]);
                a[n-1] -= a[i];
                }
            }
        }
    }
    cout << a[n-1] << endl;
    rep(i,n-1) cout << ans[i].first << " " << ans[i].second << endl;
}

Measured memory_bytes: 4336
#include<bits/stdc++.h>				//written by thidailoc
using namespace std;				//team three wolves
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pll;
#define fu(i,a,b)   for(ll i=a;i<b;i++)
#define fd(i,b,a)   for(ll i=b;i>=a;i--)
#define ms(a,x) 	memset(a, x, sizeof a)
#define prec(n) 	fixed<<setprecision(n)
#define pb(i)	    push_back(i)
#define popb()    	pop_back()
#define sc(a) 	    cin>>a
#define sc2(a,b)    cin>>a>>b
#define pr(a)	    cout<<a<<" "
#define pr2(a,b)    cout<<a<<" "<<b<<" "
#define lowb(a,n,x) lower_bound(a,a+n,x) -a
#define lowb2(a,x)	lower_bound(a.begin(),a.end(),x) -a.begin()
#define all(x) 		(x).begin(), (x).end()
#define sz(a) 		(ll)a.size()
#define le			length()						
#define re 			return 	
#define	mp(a,b)  	make_pair(a,b)
#define se			second
#define fi			first 
#define debug(x)    cerr << #x << " = " << x << endl
#define INPUT       freopen("locin.txt", "r", stdin)
#define OUTPUT      freopen("locout.txt", "w", stdout)

double const eps = 1e-6;
ll const Base=1e9+7,oo=1e17;
ll arr[1000000],mark[10000000];


ll Solves(){
//	INPUT;
	ll n,k,cnt=0,ans=0,x=0,p,test,sum=0,query,a,b,c,d;
	// PROCESSING IN HERE
	sc(n);
	string s,t;
	sc(s);
	fu(i,0,s.le){
		if(s[i]=='('){
			if(x>0){
				cnt+=x;
				x=-1;	
			}
			else x--;
		}
		else x++;			
	}
	t.clear();
	if(x>0)cnt+=x;
	while(cnt--)t.pb('(');
	s=t+s;
	
	x=cnt=0;
	fd(i,s.le-1,0){
		if(s[i]==')'){
			if(x>0){
				cnt+=x;
				x=-1;	
			}
			else x--;
		}
		else x++;			
	}
	t.clear();
	if(x>0)cnt+=x;
	while(cnt--)t.pb(')');
	s+=t;
	
	pr(s);
	re 0;
}


int main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	Solves();
}

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

using namespace std;

string inv (string a) {
  string r = "";
  for (int i = 2; i > 0 - 1; --i) r += a[i];
  return r;
}

int main () {
  string a, b;
  cin >> a >> b;
  if (inv(a) == b) cout << "YES";
  else cout << "NO";
  return 0;
}

Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,K;
ll H[101010];
ll dp[101010];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	for(i=1;i<=N;i++) {
		dp[i]=1LL<<60;
		cin>>H[i];
	}
	dp[1]=0;
	for(i=1;i<N;i++) {
		for(j=1;j<=K;j++) dp[i+j]=min(dp[i+j],dp[i]+abs(H[i]-H[i+j]));
	}
	cout<<dp[N]<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, t;
    cin >> n >> t;
    pair<int, int> a[n];
    for(int i = 0; i < n; i++)
        cin >> a[i].first >> a[i].second;
    vector<int> dp(t + 3002);
    sort(a, a + n);
    for(int i = 0; i < n; i++){
        for(int j = t - 1; j >= 0; j--)
            dp[j + a[i].first] = max(dp[j + a[i].first], dp[j] + a[i].second);
    }
    int ans = 0;
    for(int i = 0; i < dp.size(); i++)
        ans = max(ans, dp[i]);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3604
#include<iostream>
using namespace std;
int main(){
    int N,D,i,j,k,s,n,e,f;
    cin>>N>>D;
    int a[N][D];
    for(i=0;i<N;i++){
        for(j=0;j<D;j++)
            cin>>a[i][j];
    }
    n=0;
    for(i=0;i<N;i++){
        for(j=i+1;j<N;j++){
            s=0;
            for(k=0;k<D;k++){
                e=a[i][k]-a[j][k];
                f=e*e;
                s=s+f;
            }
            for(k=0;k<=s;k++){
                if(k*k==s)
                    n=n+1;
            }
        }
    }
    cout<<n;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(void){
	
	int x,y;
	while(1){
		scanf("%d %d",&x,&y);
		if (x == 0 && y == 0) break;
		if (x <= y){
			printf("%d %d\n",x,y);
		}
		if (x > y){
			printf("%d %d\n",y,x);
		}
	}
	return 0;
}

Measured memory_bytes: 600
#include <iostream>
#include <vector>

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

	long sum = 0;
	std::vector<int> abox(100000);
	for (int i = 0; i < n; ++i)
	{
		int v;
		std::cin >> v;

		sum += v;
		++abox[v];
	}

	int q;
	std::cin >> q;

	std::vector<std::pair<int, int>> bc(q);
	for (int i = 0; i < q; ++i)
	{
		std::cin >> bc[i].first;
		std::cin >> bc[i].second;
	}

	std::vector<long> s(q);
	for (int i = 0; i < q; ++i)
	{
		const int delta = abox[bc[i].first];
		abox[bc[i].first] = 0;
		abox[bc[i].second] += delta;

		sum += delta * (bc[i].second - bc[i].first);

		std::cout << sum << std::endl;
	}
}


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

int main(){
  int N;
  int count = 0;
  cin >> N;
  vector<int> A(N);
  for(int i = 0; i < N; i++){
    int x;
    scanf("%d", &x);
    A[i] = x;
  }
  sort(A.begin(), A.end());
  int a = 0;
  for(int i = 0; i < N; i++){    
    if(A[i] == a){
      count = 1;
      break;
    }
    a = A[i];
  }  
  if(count == 1){
    cout << "NO" << "\n";
  }else{
    cout << "YES" << "\n";
  }  
 return 0; 
}

Measured memory_bytes: 1024
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;
  vector<ll> veca(n + 1), vecb(n);
  for (auto &v : veca)
    cin >> v;
  for (auto &v : vecb)
    cin >> v;

  ll vsum = 0;
  for (ll i = 0; i < n; ++i) {
    ll a = veca[i];
    ll b = vecb[i];
    if (b <= a) {
      vsum += b;
    }
    else {
      vsum += min(max(a, 0LL) + veca[i+1], b);
      veca[i+1] -= b - max(a, 0LL);
    }
  }

  cout << vsum << endl;
}


Measured memory_bytes: 1792
import collections
S=int(input())
D=[]
for i in range(S):
   D.append(input())
D=collections.Counter(D)
N=int(input())
for i in range(N):
   J=input()
   D[J]-=1
N = D.most_common()[0][1]
if 0 < N:
   print(N)
else:
   print(0)

Measured memory_bytes: 3316
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from itertools import permutations, combinations,combinations_with_replacement,product
from fractions import Fraction as frac
import bisect
import heapq

import logging
logging.basicConfig(level=logging.DEBUG)

import sys
#input = sys.stdin.readline().rstrip

setrecursionlimit(1000000)

MOD = 10**9+7
INF = float('inf')

def main():
    N=int(input())
    S=input()

    ans=1
    pre_s=S[0]
    for i in range(1,len(S)):
        s=S[i]
        if pre_s!=s:
            ans+=1
        pre_s=s

    print(ans if ans !=0 else 1)
main()

Measured memory_bytes: 71404
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int main(){
    int n; cin>>n;
    int a[n]; double ave=0;
    rep(i,n){
        cin>>a[i];
        ave+=a[i];
    }
    ave/=n;
    double diff=INF; int ans=n;
    rep(i,n){
        if(diff>abs(a[i]-ave)){
            ans=i;
            diff=abs(a[i]-ave);
        }
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
q,h,s,d=map(int,input().split())
n=int(input())
num1=n%2
num2=n//2
num10=min(q*4,h*2,s)
num20=min(q*8,h*4,s*2,d)
ans=0
if num1==1:
    ans+=num10
ans+=num2*num20
print(ans)


Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>

using namespace std;

int main(){
	int n[5];
	int k;
	int c = 0;
	for (int i = 0; i < 5; i++) {
		cin >> n[i];
	}
	cin >> k;
	for (int i = 0; i < 5; i++) {
		for (int j = i; j < 5; j++) {
			int d = n[i] - n[j];
			if (d < 0)d = d * (-1);
			if (d > k)c++;
		}
	}
	if (c == 0)cout << "Yay!";
	else cout << ":(";

}

Measured memory_bytes: 256
class MergeSort
  def initialize
    @cnt = 0
    @sentinel = 2000000000
  end

  def solve
    n = gets.chomp.to_i
    s = gets.scan(/\d+/).map(&:to_i)

    merge_sort(s, 0, n)
    return [s.join(" "), @cnt]
  end

  def merge_sort(a, left, right)
    if left + 1 < right
      mid = (left + right) / 2
      merge_sort(a, left, mid)
      merge_sort(a, mid, right)
      merge(a, left, mid, right)
    end
  end

  def merge(a, left, mid, right)
    n1 = mid - left
    n2 = right - mid

    l = Array.new(n1+1)
    r = Array.new(n2+1)

    (0...n1).each do |i|
      l[i] = a[left + i]
    end

    (0...n2).each do |i|
      r[i] = a[mid + i]
    end

    l[n1] = @sentinel
    r[n2] = @sentinel

    i = 0
    j = 0

    (left...right).each do |k|
      if l[i] <= r[j]
        a[k] = l[i]
        i += 1
      else
        a[k] = r[j]
        j += 1
      end
      @cnt += 1
    end
  end
end

if $0 == __FILE__
  puts MergeSort.new.solve
end

Measured memory_bytes: 205604
import java.util.*;
public class Main 
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		List<Integer> a = new ArrayList<Integer>();	
		for(int i = 0; i < 3; ++i)
		{
			a.add(sc.nextInt());
		}
		Collections.sort(a);
		for(int i = 0; i < 2; ++i)
		{
			System.out.printf("%d ", a.get(i));
		}
		System.out.println(a.get(2));
	}
}

Measured memory_bytes: 17336
#include <iostream>
#include <string>
#include <math.h>
#include <stdio.h>
#define PI 3.1415926535897932
using namespace std;

int main(){
	int a,b,C;
	double S,h,L;
	double X;
	cin >>a>>b>>C;
	X=(double)C*PI/180.0;
	h=b*sin(X);
	S=h*a/2;
	L=sqrt((double)((a*a)+(b*b)-2*a*b*cos(X)));
	L+=(a+b);
	printf("%.6f\n",S);
	printf("%.6f\n",L);
	printf("%.6f\n",h);
	return 0;
}

Measured memory_bytes: 1196
import Data.Char(digitToInt)
main = do
  input <- getLine
  let
    func str col ret
      | str == []                    = ret
      | digitToInt (head str) == col = func (tail str) (1-col) ret
      | otherwise                    = func (tail str) (1-col) (ret+1)
    ans_ = func input 0 0
    len = length input
    ans = min ans_ (len-ans_)
    in
      putStrLn $ show ans


Measured memory_bytes: 7164
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <math.h>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int n,m,k,c[200],d[200];

int main(){
	cin>>n>>m;
	for(int i=0;i<n*2;i++)c[i]=i+1;
	for(int i=0;i<m;i++){
		cin>>k;
		if(k==0){
			for(int j=0;j<n;j++)d[j*2]=c[j];
			for(int j=0;j<n;j++)d[j*2+1]=c[n+j];
			for(int j=0;j<n*2;j++)c[j]=d[j];
		}
		else{
			for(int j=0;j<n*2-k;j++)d[j]=c[k+j];
			for(int j=0;j<k;j++)d[n*2-k+j]=c[j];
			for(int j=0;j<n*2;j++)c[j]=d[j];
		}
	}
	for(int i=0;i<n*2;i++)cout<<c[i]<<endl;
}

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

int main(){
    string numbers;
    int ans = 0;

    cin >> numbers;

    if(numbers[0] == '1') ans++;
    if(numbers[1] == '1') ans++;
    if(numbers[2] == '1') ans++; 

    cout << ans << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <cmath>
#include <stack>
#include <limits>

using namespace std;
using ll = long long int;
ll inf = numeric_limits<ll>::max();


template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto i = v.begin(); i != v.end(); i++) {
    os << *i << " ";
  }
  return os;
}


void solve(long long N, long long M, long long R, std::vector<long long> r, std::vector<long long> A, std::vector<long long> B, std::vector<long long> C){
  vector<bool> used(N+1);
  vector<ll> d(N+1);
  vector<vector<ll>> cost(N+1, vector<ll>(N+1, inf));
  vector<vector<ll>> dd(N+1, vector<ll>(N+1, inf));
  for(ll i=0; i<M; i++) {
    cost[A[i]][B[i]] = C[i];
    cost[B[i]][A[i]] = C[i];
  }
  for(ll i=0; i<R; i++) {
    for(ll j=i+1; j<R; j++) {
      fill(d.begin(), d.end(), inf);
      fill(used.begin(), used.end(), false);
      for(ll k=0; k<R; k++) {
	if(r[k]==r[i] or r[k]==r[j]) continue;
	used[r[k]] = true;
      }
      d[r[i]] = 0;
      while(true) {
	ll v = -1;
	for (int u = 1; u <= N; u++) {
	  if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
	}
	if(v == -1) break;
	used[v] = true;
	for (int u = 1; u<=N; u++) {
	  if(cost[v][u]==inf) continue;
	  d[u] = min(d[u], d[v]+cost[v][u]);
	}
      }
      dd[r[i]][r[j]] = d[r[j]];
      dd[r[j]][r[i]] = d[r[j]];
    }
  }
  sort(r.begin(), r.end());
  ll ans = inf;
  do {
    ll sum = 0;
    for(ll i=0; i+1<R; i++) {
      sum += dd[r[i]][r[i+1]];
      if(sum == inf) break;
    }
    ans = min(ans, sum);
  } while(next_permutation(r.begin(), r.end()));
  std::cout << ans << std::endl;
}

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



Measured memory_bytes: 1408
import itertools
N,H = map(int,input().split())
A = []; B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a); B.append(b)
B.sort(reverse=True)
ans = float('inf')
max_a = max(A)
acc_B = list(itertools.accumulate(B))
for k in range(N):
    damage = 0
    cnt = k+1
    damage += acc_B[k]
    cnt += -(-(H-damage) // max_a) if H-damage > 0 else 0
    if cnt < ans:
        ans = cnt
print(ans)

Measured memory_bytes: 15800
# 筐体の手が'k'回前と同じ場合, その回は勝てないが, 'k'回後は勝てる
# 筐体が出した各回の手をメモ, ただし'k'回前と同じ場合のみ ’-1’をメモする
# 勝ち手が'k'回前と同じ場合は加算なし, それ以外('-1'を含む)は勝ち点を加算 

n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()

ans = 0
memo = []
for i in range(n):
    if i < k or t[i] != memo[i-k]:
        memo.append(t[i])
        if t[i] == 'r': ans += p
        elif t[i] == 's': ans += r
        else: ans += s

    else:
        memo.append('-1')

print(ans)


Measured memory_bytes: 9956
<?php
fscanf(STDIN, "%d", $n);
$d = explode(" ", trim(fgets(STDIN)));
$sum = 0;
for ($i = 0; $i < $n; $i++) for ($j = 0; $j < $n; $j++) if ($i < $j) $sum += $d[$i] * $d[$j];
echo $sum;
?>

Measured memory_bytes: 4604
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
    long long  a, b, c;
    cin >> a >> b >> c;
    
    if (c <= a+b) {
        cout << "No" << endl;
        return 0;
    }
    
    if (c*c + a*a + b*b - 2*a*b - 2*b*c - 2*c*a > 0) {
        cout << "Yes" << endl;
    }
    else 
        cout << "No" << endl;

    return 0;
}

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

using namespace std;

string s, t;
int main() {
	cin >> s >> t;
	for(int i = 1; i <= s.size(); i++) {
		string tmp = "";
		for(int j = 0; j < s.size() - 1; j++) {
			tmp += s[j];
		}
		tmp = s[s.size() - 1] + tmp;
		s = tmp;
		if(s == t) {
			cout << "Yes" << endl;
			return 0;
		}
	}
	cout << "No" << endl;
	return 0;
} 

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

int N, K;
vector<int> a, grundy;
int main() {
  cin >> N >> K;
  a.resize(N), grundy.resize(K + 1, 0);
  rep(i, N) cin >> a[i];

  rep(k, K + 1) {
    set<int> s;
    for (int i = 0; i < N && k - a[i] >= 0; ++i) {
      s.insert(grundy[k - a[i]]);
    }
    int g = 0;
    while (s.find(g) != s.end()) ++g;
    grundy[k] = g;
  }

  cout << (grundy[K] ? "First" : "Second") << endl;

  return 0;
}


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

#define ll long long int
#define pb push_back
#define RONALDO            \
  ios::sync_with_stdio(0); \
  cin.tie(0);              \
  cout.tie(0);
#define endl "\n"
#define pii pair<ll, ll>
#define ff first
#define ss second
#define mod 1000000007
#define vi vector<char>
#define mi map<char, ll>
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
//LOVE U MESSI && CRistiano !!

//INSPIRATIONAL MICHAEL PHELPS && USAIN BOLT !!

int main()
{
  RONALDO;
 ll n,s,t,ans=0;
 cin>>n>>s>>t;
 if(s*t >= n)
 cout << "Yes";
 else
   cout << "No";
}

Measured memory_bytes: 3632
/*
 * author: Tran Minh Khoa
 * Ho Chi Minh University of Science
 */

#include <bits/stdc++.h>

using namespace std;

const int inf = (int) 1e9 + 7;
const long long INF = (long long) 1e18 + 7;

const int N = 123456;
const int M = 1010;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) (int) a.size()
#define all(a) a.begin(), a.end()
#define sqrt(x) (x) * (x)

#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define DEBUG(x) cerr << #x << " = " << x << '\n';
#define PR(x, a, b) { cerr << #x << " = "; REP(i, a, b) cerr << x[i] << ' '; cerr << '\n'; }
                 
int main() {
  #ifdef KM777
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
  #endif
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr); cout.tie(nullptr);
  
  int n, W;
  cin >> n >> W;
  vector<int> u(n + 1), v(n + 1);
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> u[i] >> v[i];
    sum += v[i];
  }
  vector<vector<int>> dp((n + 1), vector<int>(sum + 1));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i][j] = inf;
    }
  }
  for(int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= sum; j++) {
      if (j >= v[i]) {
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + u[i]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  } 
  for (int result = sum; result >= 0; result--) {
    if (dp[n][result] <= W) {
      cout << result << '\n';
      break;
    }
  }      
  return 0;
}

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

int main(){
  int a, b;
  cin >> a >> b;
  vector<string> s(a);
  int i;
  for(i = 0;i < a;i++){
    cin >> s.at(i);
  }
  for(i = 0;i<=(b+1);i++){
    cout << '#';
  }
  cout << endl;
  for(i = 0;i<a;i++){
    cout << '#' << s.at(i) << '#' << endl;
  }
  for(i = 0;i<=(b+1);i++){
    cout << '#';
  }
}


Measured memory_bytes: 3620
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

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

    bool haszero = false;
    int  mcount = 0; 
    int  nzmmv  = 1000000000;
    vector<int> a(n);

    long result = 0; 
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        result += abs(a[i]);
        if (a[i] < 0) {
            ++mcount;
        }
        if (abs(a[i]) < nzmmv) {
            nzmmv = abs(a[i]);
        }
    }

    if (mcount % 2 == 1){
        result -= (nzmmv * 2);
    }
    cout << result << endl;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
vector<int> divisor(int n) {
    vector<int> ans;
    for (int i = 1; i*i <= n; i++) {
        if (n % i == 0) {
            ans.push_back(i);
            if (i*i != n) ans.push_back(n/i);
        }
    }
    return ans;
}
int main()
{
    int N; cin >> N;
    vector<int> A(N+1);
    for (int i = 1; i <= N; i++) cin >> A[i];
    vector<int> B(N+1);
    for (int i = N; i >= 1; i--) {
        if (A[i]) {
            B[i] = 1;
            for (int d : divisor(i)) {
                A[d] ^= 1;
            }
        }
    }
    int sum = accumulate(begin(B), end(B), 0);
    cout << sum << endl;
    for (int i = 1; i <= N; i++) {
        if (B[i]) cout << i << ' ';
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 2432
#include<bits/stdc++.h>
#define U [1<<17]={}
#define G(a)((c=A[a]+A[P[a]]+C[a])%k?c:0)
typedef long long L;using namespace std;
map<L,L>T U;L P U,E U,Z U,H U,C U,A U,O U,X U,B U,k,n,a,b,c;char s U;
L D(L a){L d;Z[a]=1;for(map<L,L>::iterator I=T[a].begin();I!=T[a].end();I++)
  d=I->first,d-P[a]?P[d]=a,E[d]=E[a]+1,C[d]=I->second,D(d),Z[a]+=Z[d],!H[a]|Z[H[a]]<Z[d]?H[a]=d:0:0;}
L M(L i){return i?B[i]+M(i^i&-i):0;}
L S(L x,L i){for(x=G(x)+M(i++)-M(i);i<1e5;i+=i&-i)B[i]+=x;}
L K(L f){a=X[f];n=X[b];return a-n?M(O[E[a]<E[n]?a=n,n=b,b=f,n:f])-M(O[a])+G(a)+K(P[a]):abs(M(O[f])-M(O[b]));}
main(){for(cin>>n>>k;--n;T[a][b]=T[b][a]=c)cin>>a>>b>>c;
  for(D(b=0);n<*Z;n++)if(!n|H[P[a=n]]-n)for(;!b|a;S(a=H[a],O[a]=b++))X[a]=n;
  for(cin>>a;cin>>s>>a>>b;*s&2?cout<<K(a)<<endl,0:A[a]+=b,S(H[a],O[a]),a&&S(a,O[a]-1));}

Measured memory_bytes: 33388
N,K=map(x->parse(Int,x),split(readline()))
A=map(x->parse(Int,x),split(readline()))
K+=1
now=0
for a=A
	now+=a$K
end
ans=0
i=0
while K>0
	c=0
	for a=A
		c+=(a>>i)%2
    end
	if K%2==1
		ans=max(ans,now+(1<<i)*(2c-N))
		now+=(1<<i)*max(0,2c-N)
	else
		now+=(1<<i)*max(0,N-2c)
	end
	i+=1
	K>>=1
end
println(ans)


Measured memory_bytes: 154960
import sys
bundo = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
bundo.reverse()
for line in sys.stdin:
    n = int(line)
    ans = ''
    for i in bundo:
        if n >= i:
            n -= i
            ans = str(i) + ' ' + ans
    print(ans[:-1])


Measured memory_bytes: 5604
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef pair<double,double> PD;
typedef pair<int,P> ed;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
  
int main() {
  int N,L;
  cin >> N >> L;
  v(string) s(N);
  rep(i,N) {
    cin >> s[i];
  }
  sort(rng(s));
  string ans = "";
  rep(i,N) {
    ans += s[i];
  } 
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h> 
//#define <- プログラムの文字列を置換する
//typedef <- 新しい型に名前を付けるためのキーワード

using namespace std;

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

const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int INF_INT = 1e9;

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

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

template<class TYPE>
struct edge{
    TYPE to,cost; 
    edge(TYPE t,TYPE c){
        to = t;
        cost = c;
    }
};

int main(){
    int N;
    cin >> N;
    vector<ll> a(N);
    ll sum = 0;
    for(int i = 0; i < N; i++){
        cin >> a[i];
        sum += a[i];
    }
    ll res = 0;
    for(int i = 0; i < N - 1; i++){
        sum -= a[i];
        res = (res + ((a[i] % MOD) * (sum % MOD)) % MOD) % MOD;
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 4808
#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 bubbleSort(vector<int>& A);

int main()
{
    int N, count;
    cin >> N;
    vector<int> A(N);
    REP(i, N) cin >> A[i];
    count = bubbleSort(A);
    for(int i = 0; i < A.size(); i++)
    {
        if(i != 0) cout << " ";
        cout << A[i];
    }
    cout << endl;
    cout << count << endl;
    return 0;
}

int bubbleSort(vector<int>& A)
{
    bool flag = true;           // 逆の隣接要素が存在する
    int swapCnt = 0;            // 反転数
    for(int i = 0; flag; i++)   // i: 未ソート部分の先頭インデックス
    {
        flag = false;
        // 配列末尾から、未ソート部分の先頭インデックスまでループ
        // 未ソート部分の最小値を配列の先頭方向に移動させる
        for(int j = A.size() - 1; j > i; j--)
        {
            // 小さい値を移動させる
            if(A[j] < A[j - 1])
            {
                swap(A[j], A[j - 1]); 
                flag = true;
                swapCnt++;
            }
        }
    }
    return swapCnt;
}


Measured memory_bytes: 3100
'use strict';
process.stdin.on('data', function(chunk) {
  var inputs = chunk.toString().trim().split(' ');
  var a = Number(inputs[0]);
  var b = Number(inputs[1]);

  console.log(a * b, (a + b) * 2);
});
process.stdin.resume();
process.stdin.setEncoding('utf8');

Measured memory_bytes: 15332
mod io{
    use std::io::*;
    pub fn read<T: ::std::str::FromStr>()->T{
        let stdin=stdin();
        let ret=stdin.lock().bytes().map(|c|c.unwrap()as char)
        .skip_while(|c|c.is_whitespace())
        .take_while(|c|!c.is_whitespace())
        .collect::<String>().parse().ok().unwrap();
        ret
    }
}
use io::read;
fn main(){
    let n=read::<usize>();
    let a=n/100;
    let b=n%100;
    println!("{}",if 0<a&&a<13{if 0<b&&b<13{"AMBIGUOUS"}else{"MMYY"}}else{if 0<b&&b<13{"YYMM"}else{"NA"}})
}


Measured memory_bytes: 4352
x, k, d = map(int, input().split())
if x<0:
  x*=-1
if x>=k*d:
  print(x-k*d)
else:
  quo = x//d
  sur = x%d
  if (k-quo)%2==0:
    print(sur)
  else:
    print(d-sur)

Measured memory_bytes: 9048
# input
N = int( input() )
A = [int(x) for x in input().split()]

# calc 
ans = True
for a in A:
    if( a%2 == 0 ):
        mod3 = a%3
        mod5 = a%5
        if( mod3 == 0 or mod5==0 ):
            continue
        else:
            ans = False

# output      
if(ans):
    print( "APPROVED" )
else:
    print( "DENIED" )

Measured memory_bytes: 3060
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define INF (1 << 30)
#define INFL (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-10

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define mp make_pair
#define pb push_back
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }
void Taka() { cout << "Takahashi" << endl; }
void Aoki() { cout << "Aoki" << endl; }
void digit(int d) { cout << fixed << setprecision(d); }
void IOup() { cin.tie(0); ios::sync_with_stdio(false); }

vector<tuple<int, int, int>> v;
pii l[100001];

int main() {
	int n, m, s, t, c;
	cin >> n >> m;
	v.resize(n);
	for (int i = 0; i < n; ++i) {
		cin >> s >> t >> c;
		v[i] = make_tuple(s, t, c);
	}

	sort(all(v));

	int a, b;
	for (int i = 0; i < n; ++i) {
		a = get<0>(v[i]);
		b = get<1>(v[i]);
		c = get<2>(v[i]);

		for (int j = 0; j < n; ++j) {
			if (l[j].first < a || (l[j].first <= a && l[j].second == c)) {
				l[j].first = b;
				l[j].second = c;
				break;
			}
		}
	}

	int ans = 0;
	while (1) {
		if (l[ans].first == 0)
			break;
		ans++;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 1408
n,a,b,c = map(int,input().split())

ss = []
for i in range(n):
    ss.append(input())

ans = []
tower = [a,b,c]
s2num = {"AB":[0,1],"BC":[1,2],"AC":[0,2]}
num2s = {0:"A",1:"B",2:"C"}
flag =True

def check_large(l,r,t):
    if t[l]>t[r]:
        return l,r,False
    elif t[l]<t[r]:
        return r,l,False
    else:
        return l,r,True

for i in range(n):
    s = ss[i]
    l,r = s2num[s]
    big,small,f = check_large(l,r,tower)
    if f:
        if i+1 < n:
            nex = s2num[ss[i+1]]
            if big in nex:
                small,big = big,small

    if tower[big] <= 0:
        flag = False
        break
    else:
        tower[big]-=1
        tower[small]+=1
        ans.append(num2s[small])

if flag:
    print("Yes")
    for i in range(n):
        print(ans[i])
else:
    print("No")

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

int main () {
  string s,t;
  cin >> s >> t;
  t.pop_back();
  if (s==t) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 3588
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

int main(){
  int num_student;
  int num_min_pass;
  int num_max_pass;
  while(~scanf("%d %d %d", &num_student, &num_min_pass, &num_max_pass)) {
    if (num_student == 0) break;

    int score[10000];
    for (int student_i = 0; student_i < num_student; student_i++) {
      scanf("%d", &score[student_i]);
    }
    sort(score,score + num_student, greater<int>());
    int diff = -1;
    int res = 0;
    for (int pass_i = num_min_pass - 1; pass_i < num_max_pass; pass_i++) {
      int min_pass_score = score[pass_i];
      int max_fail_score = score[pass_i + 1];
      if (diff <= min_pass_score - max_fail_score) {
	diff = min_pass_score - max_fail_score;
	res = pass_i + 1;
      }
    }
    printf("%d\n",res);
  }
}

Measured memory_bytes: 3192
def main():
  s = input()[::-1]
  l = len(s)
  n = 2019
  dp = [0]*n
  wk = 0
  r = 1
  for i in range(l):
    wk += int(s[i])*r
    wk %= n
    dp[wk] += 1
    r*= 10
    r %= n
  print(dp[0]+sum([i*(i-1)//2 for i in dp]))
main()

Measured memory_bytes: 9188
a = int(input())
z = a
divide = [i for i in range(2, a) if i % 2 != 0]
divide.append(2)
divide.sort()

if a == 2:
  print(2)
  
else:
  while 1 == 1:
    for i in divide:
      start = i
      if a % i == 0:
        a += 1
        break
    if start >= z-3:
      break  
  print(a)


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

int main(){
    int n;
    cin>>n;
    vector<vector<int> > v(n,vector<int>(3));
    for(int i=0;i<n;i++)
	cin>>v[i][0]>>v[i][1]>>v[i][2];
    vector<vector<int> > dp(n,vector<int>(3,0));

    dp[0][0]=v[0][0];
    dp[0][1]=v[0][1];
    dp[0][2]=v[0][2];

    for(int i=1;i<n;i++){
	dp[i][0]=dp[i-1][1]>dp[i-1][2]? dp[i-1][1]+v[i][0]:dp[i-1][2]+v[i][0];
	dp[i][1]=dp[i-1][0]>dp[i-1][2]? dp[i-1][0]+v[i][1]:dp[i-1][2]+v[i][1];
	dp[i][2]=dp[i-1][0]>dp[i-1][1]? dp[i-1][0]+v[i][2]:dp[i-1][1]+v[i][2];
    }
    int MAX=0;
    MAX=dp[n-1][0]>MAX? dp[n-1][0]:MAX;
    MAX=dp[n-1][1]>MAX? dp[n-1][1]:MAX;
    MAX=dp[n-1][2]>MAX? dp[n-1][2]:MAX;
    cout<<MAX<<endl;
    return 0;
}

Measured memory_bytes: 11136
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 10000000000000
int dp[3001][3001];
int sum[3001][3001];

int solve(int i, int j) {
    if(i == j) return 0;
    if(dp[i][j] != -1) return dp[i][j];
    int least = inf;
    for(int k=i; k<j; k++) least = min(least, solve(i, k) + solve(k+1, j) + sum[i][j]);
    return dp[i][j] = least;
}

void prepro(vector<int>& vec, int N) {
    for(int i=1; i<=N; i++) {
        for(int j=i; j<=N; j++) sum[i][j] = vec[j] + ((i == j) ? 0 : sum[i][j-1]);
    }
}

int32_t main() {
    int N; cin>>N;
    vector<int> vec(N+1);
    for(int i=1; i<=N; i++) cin>>vec[i];
    memset(dp, -1, sizeof dp);
    prepro(vec, N);
    cout<<solve(1, N)<<endl;
    return 0;
}


Measured memory_bytes: 76212
#!/usr/bin/perl

my ($H,$W) = map { $_ - 0 } split(/\s+/,<STDIN>);

my $ha = ($H >> 1);
my $wa = ($W >> 1);

my $c = $ha * $wa * 2;

if( ( $W % 2 ) == 1 ){
  $c += $ha;
}
if( ( $H % 2 ) == 1 ){
  $c += $wa;
}
if( ( $W % 2 ) == 1 and ( $H % 2 ) == 1 ){
  $c ++;
}

if( $H == 1 or $W == 1 ){
  $c = 1;
}

print "$c\n";

exit(0);



Measured memory_bytes: 512
import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val n = sc.nextInt()
  val a = (1..n).map { sc.nextLong() }

  val ans = calc(a)

  println(ans)
}
 
private fun calc(a: List<Long>): Long {
  if (a.contains(0L)) return 0
  val max = 1_000_000_000_000_000_000

  var ret = 1L
  for (ai in a) {
    val tmp = ret * ai
    if (tmp > max) return -1
    if (tmp < ret) return -1
    ret = tmp
  }

  return ret
}


Measured memory_bytes: 61780
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("YES");}else{puts("NO");}
const int MX = 200005;

ll dp[MX];

int main() {
  int n,w;
  scanf("%d%d",&n,&w);
  rep(i,MX) dp[i] = LINF;
  dp[0] = 0;
  rep(i,n) {
    int a,b;
    scanf("%d%d",&a,&b);
    drep(j,MX-b) {
      mins(dp[j+b], dp[j]+a);
    }
  }
  int ans = 0;
  rep(i,MX) if (dp[i] <= w) ans = i;
  cout<<ans<<endl;
  return 0;
}






















Measured memory_bytes: 1792
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<long long,long long>
#define vl vector<long long>
#define vll vector<pll>
#define vi vector<int>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define tcase int tt; cin>>tt; while(tt--)
#define tcase2 int tt; cin>>tt; for(int qq=1;qq<=tt;qq++)
using namespace std;
typedef long long int ll;
typedef  unsigned long long int ull;
typedef long double ld;
const int MAX = 1000010;
const int MOD2 = 998244353;
const int MOD = 1000000007; 
const int INF = 1e8;    
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;
// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()   
#define trav(a, x) for(auto& a : x)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr<< *it << " = " << a << endl;
	err(++it, args...);
}
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
    sws
    int n,k;
    cin>>n>>k;
    vi a(n);
    forn(i,n)
        cin>>a[i];
    sort(all(a));
    int ans=0;
    forn(i,k)
        ans+=a[i];
        cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 3656
/* @Author: Shubham Anand */

#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <limits>
#include <chrono>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>

#define FILEIN          freopen("input.txt", "r", stdin)
#define FILEOUT         freopen("output.txt", "w", stdout)
#define FASTIO          std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::cout.tie(NULL)
#define PAUSE           while(1){}
#define NEWLINE         std::cout << "\n"
#define EDGES           std::unordered_map<std::pair<int,int>,int>
#define PB(N)           push_back(N)

/* Constants */
const unsigned int MODULO = 1e9+7;
const unsigned int INF = std::numeric_limits<int>::max();
const int NINF = std::numeric_limits<int>::min();

/* Input Output */
template <class T> void write(const T &a, char del='\n')        { if(del=='~')std::cout<<a; else std::cout<<a<<del; }
template <class T> void line(const T &w)                        { for(int i=0;i<(w+1)/2;i++)write('_','_');NEWLINE; }
template <class T> void read(T &a)                              { std::cin >> a; }

/* Modular Ops */
template <class T> T add(T a, T b, T m) { a=a%m; b=b%m; return (a+b)%m; }
template <class T> T mul(T a, T b, T m) { a=a%m; b=b%m; return (a*b)%m; }

/* Basic Algorithms */
template <class T> T gcd(T a, T b)      { if(b>a){return gcd(b,a);} if(b==0){return a;} return gcd(b, a%b); }
template <class T> T bexp(T a, T b, T m){ if(b==1){return a;}if(b&1){return mul(a,bexp(a,b-1,m),m);}T c=bexp(a,b/2,m); return mul(c,c,m);}
template <class T> T inv(T a, T m)      { return bexp(a,m-2,m); }

/* Commonly used data structures */
namespace ds {
    template<class T> struct Point{ T x,y,z; Point(T _x=0,T _y=0,T _z=0):x(_x), y(_y), z(_z){}; };
    template<class T> struct Graph{ std::vector<T> *adj; Graph(T n){adj=new std::vector<T>[n];} void add(T u, T v, bool d=0){adj[u].PB(v);if(!d){adj[v].PB(u);}} void w_add(T u, T v, T w, bool d=0){adj[u].PB(std::make_pair(v,w)); if (!d){adj[v].PB(std::make_pair(u,w));}}};
}

/* Function to calculate runtime */
void time(void func()) {
    std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
    start = std::chrono::high_resolution_clock::now();
    func();
    end = std::chrono::high_resolution_clock::now();
    long long elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
    std::cout << "\tElapsed Time: " << elapsed_time << "ms\n";
}

/* Declare any global objects here */

/************** END ****************/

void solve() {
    
    int N; read(N);
    int h[N];
    for (int i=0; i<N; i++) { read(h[i]); }

    int dp[N];
    dp[0] = 0;
    dp[1] = abs(h[1]-h[0]);

    for (int i=2; i<N; i++) {
        dp[i] = std::min(dp[i-2]+abs(h[i]-h[i-2]), dp[i-1]+abs(h[i]-h[i-1]));
    }

    write(dp[N-1]);

}

signed main() {

    // FILEIN; 
    // FILEOUT;
    FASTIO;

    int T = 1;
    // Uncomment to read testcases
    // read(T);
    
    while (T--) {
        // time(solve);
        solve();
    }

    return 0;
}

Measured memory_bytes: 1024
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

int main(void){
    string s, t;
    cin >> s >> t;
    vector<vector<int>> dp(3010, vector<int>(3010));
    for (int i = 0; i < (int)s.size(); i++)
    {
        for (int j = 0; j < (int)t.size(); j++)
        {
            if(s[i] == t[j]) dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1);
            dp[i+1][j+1] = max(dp[i+1][j+1], max(dp[i][j+1], dp[i+1][j]));
        }
    }
    string res = "";
    int i = (int)s.size(), j = (int)t.size();
    while (i > 0 && j > 0)
    {
        if(dp[i][j] == dp[i-1][j]){
            i--;
        }
        else if(dp[i][j] == dp[i][j-1]){
            j--;
        }
        else
        {
            res = s[i-1] + res;
            i--;
            j--;
        }
    }
    cout << res << endl;
}


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

using namespace std;

int a[5];
vector<int> g[5];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 3; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= 4; i++) {
    a[g[i].size()]++;
  }
  if (a[1] == 2 && a[2] == 2) {
    puts("YES");
  } else {
    puts("NO");
  }
  return 0;
}


Measured memory_bytes: 256
# D - Built?
import sys
import heapq

# input
N = int(input())

X, Y = [], []
for n in range(N):
    x, y = map(int, sys.stdin.readline().strip().split())
    X.append((x, n))
    Y.append((y, n))

# build matrix
G = [[] for _ in range(N)]

X.sort()
Y.sort()
for i in range(N - 1):
    d_u, u = X[i]
    d_v, v = X[i + 1]

    G[u].append((d_v - d_u, v))
    G[v].append((d_v - d_u, u))

    d_u, u = Y[i]
    d_v, v = Y[i + 1]

    G[u].append((d_v - d_u, v))
    G[v].append((d_v - d_u, u))


# 最小全域木
q = []
fp = [False] * N  # 探索済み
fp[0] = True

for d, u in G[0]:
    heapq.heappush(q, (d, u))

ans = 0
while q:
    d, u = heapq.heappop(q)

    if fp[u]:
        continue
    fp[u] = True
    ans += d

    for d_v, v in G[u]:
        if fp[v]:
            continue
        heapq.heappush(q, (d_v, v))

print(ans)


Measured memory_bytes: 117616
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<cstring>
using namespace std;
const int maxn=1e3+5;
int num[4][4];
int ans[4][4];

int main()
{
	for(int i=1;i<=3;i++)
	{
		for(int j=1;j<=3;j++) scanf("%d",&num[i][j]);
	}
	int n;
	cin>>n;
	memset(ans,0,sizeof(ans));
	for(int i=0;i<n;i++)
	{
		int tt;
		scanf("%d",&tt);
		for(int i=1;i<=3;i++)
		{
			for(int j=1;j<=3;j++) 
			{
				if(tt==num[i][j]) ans[i][j]=1;
			}
		}
	}
	int fl=0;
	for(int i=1;i<=3;i++)
	{
		int sum1=0,sum2=0;
		for(int j=1;j<=3;j++)
		{
			sum1=sum1+ans[i][j];
			sum2=sum2+ans[j][i];
		}
		if(sum1>=3||sum2>=3) fl=1;
		if(fl) break;
	}
	int sum1=0,sum2=0;
	for(int i=1;i<=3;i++)
	{
		sum1+=ans[i][i];
		sum2+=ans[i][4-i];
	}
	if(sum1>=3||sum2>=3) fl=1;
	if(fl) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}

Measured memory_bytes: 256