akhauriyash/Code-Regression huggingface dataset:
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
bc = [[0] * 2] * m

for i in range(m):
    bc[i] = list(map(int, input().split()))

a.sort()
bc = sorted(bc, key=lambda x: -x[1])
new = []

cnt = 0

for e in bc:
    new += [e[1]] * e[0]
    cnt += e[0]

    if (n <= cnt):
        break

for i in range(min(n, cnt)):
    a[i] = max(a[i], new[i])

print(sum(a))


Measured memory_bytes: 83180
import Control.Monad

main :: IO ()
main = do
  n <- readLn
  l <- replicateM n $ do
    [t, a] <- map read . words <$> getLine
    return (t, a)
  print $ uncurry (+) $ solve l (1, 1)

solve :: [(Int, Int)] -> (Int, Int) -> (Int, Int)
solve [] (t, a) = (t, a)
solve ((t, a):xs) (pt, pa) = solve xs (m * t, m * a)
  where
    nt = if pt `mod` t == 0 then pt `div` t else pt `div` t + 1
    na = if pa `mod` a == 0 then pa `div` a else pa `div` a + 1
    m = max nt na


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

int main() {
    ll A, B; cin >> A >> B;
    ll C = B-A+1;
    if(A%2 == 1)C--;
    ll ans;
    if(C%4 == 0){
        if(A%2 == 1){
            cout << A << endl;
        } else {
            cout << 0 << endl;
        }
    } else if (C%4 == 1){
        if(A%2 == 1){
            ans = A ^ B;
            cout << ans << endl;
        } else {
            cout << B << endl;
        }
    } else if (C%4 == 2){
        if(A%2 == 1){
            ans = A ^ 1;
            cout << ans << endl;
        } else {
            cout << 1 << endl;
        }
    } else {
        if(A%2 == 1){
            ans = A ^ 1 ^ B;
            cout << ans << endl;
        } else {
            ans = 1 ^ B;
            cout << ans << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 256
import bisect
from math import factorial as fact
import sys
input = sys.stdin.readline
 
N = int(input())
prime = []
p_flag = [False] * (101)
p_flag[0] = p_flag[1] = True
 
for i in range(2, 101):
    if not p_flag[i]:
        prime.append(i)
        for j in range(2*i, 101, i):
            p_flag[j] = True
 
M = len(prime)
factor = [0]*M
 
for i in range(2, N+1):
    for j in range(M):
        tmp = prime[j]
        if tmp > i:
            break
        while i % tmp == 0:
            factor[j] += 1
            tmp *= prime[j]
 
factor = factor[::-1]
point74 = M - bisect.bisect_left(factor, 74)
point24 = M - bisect.bisect_left(factor, 24)
point14 = M - bisect.bisect_left(factor, 14)
point4 = M - bisect.bisect_left(factor, 4)
point2 = M - bisect.bisect_left(factor, 2)
 
if N < 10:
    ans = 0
else:
    ans = fact(point4)//(fact(point4-2)*fact(2)) * (point2-2)
    if point14 > 0:
        ans += point14 * (point4-1)
    if point24 > 0:
        ans += point24 * (point2-1)
    if point74 > 0:
        ans += point74
 
print(ans)

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

int main() {
	string s;
	cin>>s;
	int n=s.length();
	int arr[n];
	for(int i=0;i<n;i++)
	arr[i]=0;
	int r=0;
	for(int i=0;i<n;i++)
	{
		if(s[i]=='R')
		r=i;
		else
		{
			int cnt=i-r;
			if(cnt%2)
			{
				arr[r+1]++;
			}
			else
			arr[r]++;
		}
	}
	int l=n-1;
	for(int i=n-1;i>=0;i--)
	{
		if(s[i]=='L')
		l=i;
		else
		{
			int cnt=l-i;
			if(cnt%2)
			{
				arr[l-1]++;
			}
			else
			arr[l]++;
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	return 0;
}

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

#define ff first
#define ss second
#define pb push_back

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

const int N = 100005;

int v[N] = {0, 300000, 200000, 100000};

int main() {
	int x, y;
	scanf("%d %d", &x, &y);
	printf("%d\n", v[x] + v[y] + (x == y && y == 1 ? 400000 : 0 ));
}


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

int main()
{
  int N; cin >> N;
  string S; cin >> S;
  
  if (S.length() % 2 == 1)
  {
    cout << "No" << endl;
  }
  
  else if (S.substr(0, S.length() / 2) == S.substr(S.length() / 2))
  {
    cout << "Yes" << endl;
  }
  
  else
  {
    cout << "No" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 256
import heapq
N,M=map(int,input().split())
A=list(map(lambda x:x*-(1),list(map(int,input().split()))))
heapq.heapify(A)
for i in range(M):
    num=(heapq.heappop(A)*(-1)/2)
    heapq.heappush(A, num*-1)



A=list(map(int,A))
print(-sum(A))


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

int main(){
  long long K,A,B;
  cin >> K >> A >> B;
  if(K>A && A<=B){cout << -1 << endl; return 0;}
  else if(A<=B){cout << 1 << endl; return 0;}
  long long q=(K-B+A-B-1)/(A-B);
  cout << q*2-1 << endl;
}


Measured memory_bytes: 256
#define FS first
#define SC second

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

int h, w, ans = 0;
char mp[60][60];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	int h, w, ans = 0;
	char mp[60][60];
	
	cin >> h >> w;
	
	for(int i = 0; i < h; i++)
	{
		cin >> mp[i];
	}//for i
	
	for(int i = 0; i < h; i++)
	{
		for(int j = 0; j < w; j++)
		{
			if(mp[i][j] == '.')
				ans++;
		}//for j
	}//for i
	
	int x, y, nx, ny, X[] = {1, -1, 0, 0}, Y[] = {0, 0, 1, -1};
	bool vis[60][60] = {false};
	queue<PII> que[2];
	
	que[0].emplace(0, 0);
	
	for(int i = 0; !que[i & 1].empty(); i++)
	{
		while(!que[i & 1].empty())
		{
			x = que[i & 1].front().FS, y = que[i & 1].front().SC;
			que[i & 1].pop();
			
			if(x == h - 1 && y == w - 1)
			{
				cout << ans - i - 1;
				return 0;
			}//if
			
			if(vis[x][y])
				continue;
			
			vis[x][y] = true;
			
			for(int j = 0; j < 4; j++)
			{
				nx = x + X[j], ny = y + Y[j];
				
				if(0 <= nx && nx < h && 0 <= ny && ny < w && mp[nx][ny] == '.' && !vis[nx][ny])
					que[1 - (i & 1)].emplace(nx, ny);
			}//for j
		}//while
	}//for i
	
	cout << "-1";
	
	return 0;
}//main


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

int a[11];
int b[11];
int dp[11][1010]; // xテ、ツサツ・テ、ツクツ凝」ツ?ョテ」ツつォテ」ツδシテ」ツδ嘉」ツ?ョテ」ツ?ソテ」ツつ津ァツ板ィテ」ツ??」ツ?ヲテヲツ閉ーyテ」ツ?ォテ」ツ?ェテ」ツつ凝」ツδ妥」ツつソテ」ツδシテ」ツδウテヲツ閉ー

int main() {
  int m;
  
  while( cin >> m && m ) {
    for( int i=0; i<m; i++ ) {
      cin >> a[i] >> b[i];
    }
    
    fill(dp[0], dp[8], 0);
    dp[0][0] = 1;
    for( int i=0; i<m; i++ ) {
      for( int j=0; j<1001; j++ ) {
	if( dp[i][j] ) {
	  for( int k=0; k<=b[i]; k++ ) {
	    if( j+k*a[i] <= 1000 ) dp[i+1][j+k*a[i]] += dp[i][j];
	  }
	}
      }
    }
    
    int g;
    cin >> g;
    for( int i=0; i<g; i++ ) {
      int q;
      cin >> q;
      cout << dp[m][q] << endl;
    }
  }
  
  return 0;
}

Measured memory_bytes: 1196
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;

int N;

int main(void){
    
    scanf("%d", &N);
    vector<ll> a(N);
    for(int i = 0; i < N; ++i) scanf("%lld", &a[i]);
    
    vector<ll> up, down; up = down = a;
    sort(up.begin(), up.end()), sort(down.begin(), down.end(), greater<ll>());
    
    vector<ll> v1(N, 0), v2(N, 0);
    if(N % 2){
        int cnt = 0, idx = 0;
        for(int i = N / 2; i >= 0 && i < N; i += (cnt % 2) ? cnt * 2 : -(cnt * 2)) ++cnt, v1[i] = up[idx], v2[i] = down[idx++];
        cnt = 0, idx = 0;
        for(int i = N / 2 - 1; i >= 0 && i < N; i += (cnt % 2) ? cnt * 2 : -(cnt * 2)) ++cnt, v1[i] = down[idx], v2[i] = up[idx++];
        
    }
    else{
        int cnt = 0, idx = 0;
        for(int i = N / 2 - 1; i >= 0 && i < N; i += (cnt % 2) ? cnt * 2 : -(cnt * 2)) ++cnt, v1[i] = up[idx], v2[i] = down[idx++];
        cnt = 0, idx = 0;
        for(int i = N / 2; i >= 0 && i < N; i += (cnt % 2) ? -(cnt * 2) : cnt * 2) ++cnt, v1[i] = down[idx], v2[i] = up[idx++];
    }
    
    ll sum1 = 0, sum2 = 0;
    for(int i = 0; i < N - 1; ++i) sum1 += abs(v1[i] - v1[i + 1]), sum2 += abs(v2[i] - v2[i + 1]);
    printf("%lld\n", max(sum1, sum2));
    
    return 0;
}


Measured memory_bytes: 4224
def intlog10(n):
    if n <= 0:
        return -1
    i = 0
    while 10**i <= n:
        i += 1
    return i-1

N, M = map(int, input().split())
A = [int(a) for a in input().split()]
    
L = [2, 5, 5, 4, 5, 6, 3, 7, 6]
B = [[A[i], L[A[i]-1]] for i in range(M)]

best = sorted(B, key = lambda x: x[1]*10-x[0])[0]

adj = 0
if N > 500:
    adj = (N - 500)//best[1]
    N -= best[1] * adj

X = [0] + [-10**9] * N
for i in range(1, N+1):
    if i > 50 and i < N-50:
        X[i] = X[i-best[1]]*10 + best[0]
    else:
        for b in B:
            if b[1] <= i:
                if X[i-b[1]] == 0:
                    X[i] = max(X[i], b[0])
                else:
                    X[i] = max(X[i], X[i-b[1]]*10 + b[0], X[i-b[1]] + b[0] * (10**intlog10(X[i-b[1]])))

ans = str(X[N])
if adj > 0:
    ans = ans[:50] + str(best[0]) * adj + ans[50:]

print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
 
using namespace std;
 
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    Int(n);
    vi data(n);
    rep(i, n) {
        cin >> data[i];
    }

    int ans = -INF, small = data[0];
    Rep(i, n) {
        ans = max(ans, data[i] - small);
        small = min(small, data[i]);
    }
    cout << ans << endl;


    return 0;
}

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

int main() {
	int a,b,t;
  	cin>>a>>b>>t;
  
  	
  
  	cout<<b*(t/a)<<endl;
  
}

Measured memory_bytes: 3632
import java.util.Scanner;
 
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int A = sc.nextInt();
        int B = sc.nextInt();
        int C = sc.nextInt();
        
        if(A + B >= C){
            System.out.println("Yes");
        }else if(A + B < C){
            System.out.println("No");
        }
    }
}

Measured memory_bytes: 21716
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>

using namespace std;

long long int n, a, b, t;

int gcd(int m, int n){
	while (m != n)
	{
		if (m > n) m = m - n;
		else         n = n - m;
	}
	return m;
}

int main(){
	cin >> n;
	cin >> a >> b;
	t = a*b / gcd(a, b);
	for (int i = 0; i < n - 2; i++){
		cin >> a;
		t = t*a / gcd(t, a);
	}
	cout << t << endl;
}

Measured memory_bytes: 1156
//
//  main.cpp
//  pA
//
//  Created by Hsiu-Fen Chou on 2019/8/30.
//  Copyright © 2019 Hsiu-Fen Chou. All rights reserved.
//

#include <iostream>
using namespace std;

int main() {
    int A, B;
    int maxi;
    
    while (cin >> A >> B) {
        maxi = A + B;
        if (A-B > maxi)
            maxi = A - B;
        if (A*B > maxi)
            maxi = A * B;
        cout << maxi << endl;
    }
    return 0;
}


Measured memory_bytes: 256
/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;

public class Main {
	public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        int n,s,i;
        n=sc.nextInt();
        s=sc.nextInt();
        // no=sc.nextInt();
        int a[]=new int[n];
        for(i=0;i<n;i++)
        {
            a[i]=sc.nextInt();
        }Arrays.sort(a);
        int nm=n-s;
        i=0;
        long ans=0;
        while(nm>0)
        {
            ans+=a[i];
            i++;
            nm--;
        }
        System.out.println(ans);
        
        
	}
}

Measured memory_bytes: 64892
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>


using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i,a,b) for(int i = (a); i < (int)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())

#define pb push_back
#define mp make_pair
#define mt make_tuple

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

// 2数の最小公倍数
ll getLcmSimple(ll a, ll b)
{
    return a / getGcdSimple(a, b) * b;
}

int main(void)
{
    cin.sync_with_stdio(false);
    ll N, M;
    cin >> N >> M;
    string S, T;
    cin >> S;
    cin >> T;

    auto ans = getLcmSimple(N, M);
    auto n_step = ans / N;
    auto m_step = ans / M;

    ll i = 0;
    ll j = 0;
    bool ok = true;
    while (i < ans && j < ans) {
        if (i == j) {
            // cout << "i: " << i << endl;
            if (S[i/n_step] != T[j/m_step]) {
                ok = false;
                break;
            }
            i += n_step;
            j += m_step;
        }
        else if (i < j) i += n_step;
        else if (j < i) j += m_step;
    }
    if (ok) cout << ans << endl;
    else cout << -1 << endl;

    return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define reps(i,s,n) for(int i=s; i<n; i++)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

pii cost[101][101];
int visit[101];

struct node {
	int cost, money, pos;
	node(int c, int m, int p) {
		cost = c;
		money = m;
		pos = p;
	}
};
bool operator<( const node& l, const node& r ) {
	if( l.cost == r.cost ) {
		return l.money < r.money;
	}
	return l.cost > r.cost;
}

int main() {
	int n, m, l;
	while( cin >> n >> m >> l, n||m||l ) {
		rep(i,101) rep(j,101) cost[i][j].first = cost[i][j].second = -1;
		rep(i, m) {
			int a, b, d, e;
			cin >> a >> b >> d >> e;
			cost[a][b].first = cost[b][a].first = d;
			cost[a][b].second = cost[b][a].second = e;
		}
		priority_queue<node> que;
		memset( visit, -1, sizeof(visit) );
		que.push( node(0, l, 1) );
		while( !que.empty() ) {
			node nd = que.top();
			que.pop();
			if( nd.money <= visit[nd.pos] ) continue;
			visit[nd.pos] = nd.money;
			if( nd.pos == n ) {
				cout << nd.cost << endl;
				break;
			}
			for(int i=1; i<=n; i++) {
				if( cost[nd.pos][i].second != -1 ) {
					if( visit[i] < nd.money ) {
						que.push( node(nd.cost+cost[nd.pos][i].second, nd.money, i) );
					}
					if( nd.money >= cost[nd.pos][i].first && visit[i] < nd.money-cost[nd.pos][i].first ) {
						que.push( node(nd.cost, nd.money-cost[nd.pos][i].first, i) );
					}
				}
			}
		}
	}
	return 0;
}

Measured memory_bytes: 1360
N = int(input())
c=[]
s=[]
f=[]
t=[0]*N
for i in range(N-1):
    ci, si, fi = map(int, input().split())
    c.append(ci)
    s.append(si)
    f.append(fi)

t[N-1]=0
t[N-2]=s[N-2]+c[N-2]
for i in range(N-3,-1,-1):
#    print(N-i-1)
    t[i]=c[i]+s[i]
    for j in range(i,N-2):
        if t[i]<=s[j+1]:
            t[i]=t[i+1]
            break
        else:
            if t[i]%f[j+1]!=0:
                t[i]=t[i]+(f[j+1]-t[i]%f[j+1])+c[j+1]
            else:
                t[i]=t[i]+c[j+1]
for i in range(len(t)):
    print(t[i])

Measured memory_bytes: 3188
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

type Point struct {
	T int
	X int
	Y int
}

func main() {
	s := IOInitialize()
	N := Int(s)
	points := scanNums(s, N)
	currentPoint := Point{T: 0, X: 0, Y: 0}
	for _, point := range points {
		dis := abs(abs(currentPoint.X-point.X) + abs(currentPoint.Y-point.Y))
		tDiff := point.T - currentPoint.T
		if dis > tDiff || tDiff%2 != dis%2 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

func abs(a int) int {
	return int(math.Abs(float64(a)))
}

func IOInitialize() *bufio.Scanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return s
}

func Int(s *bufio.Scanner) int {
	if !s.Scan() {
		panic("scan error")
	}

	value, err := strconv.Atoi(s.Text())
	if err != nil {
		panic("text to int error")
	}
	return value
}

func scanNums(s *bufio.Scanner, len int) (points []Point) {
	points = []Point{}
	for i := 0; i < len; i++ {
		t := Int(s)
		x := Int(s)
		y := Int(s)
		points = append(points, Point{T: t, X: x, Y: y})
	}
	return
}


Measured memory_bytes: 10752
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
using namespace std;
int m,n,sum;
char xx[101][101];
int vis[101][101];
int check(int a,int b)
{
	if(a >= m || a < 0 || b >= n || b < 0)
	{
		return 0;
	}
	return 1;
}
struct mmm
{
	int x,y;
};
int direction[4][2] = {0,1,0,-1,-1,0,1,0};
void bfs(int x,int y,char zzz)
{
	mmm vv;
	queue <mmm> qq;
	vv.x = x;
	vv.y = y;
	qq.push(vv);
	vis[x][y] = 1;
	while(!qq.empty())
	{
		vv = qq.front();
		qq.pop();
		int x0 = vv.x;
		int y0 = vv.y;
		for(int  i = 0 ; i < 4 ; i ++)
		{
			int x1 = x0 + direction[i][0];
			int y1 = y0 + direction[i][1];
			if(check(x1,y1) == 1 && vis[x1][y1] == -1 && xx[x1][y1] == zzz)
			{
				vis[x1][y1] = 1;
				vv.x = x1;
				vv.y = y1;
				qq.push(vv);
			}
		}
	}
}
int main()
{
	int a,b,c,d,e,i,j,k;
	while(~scanf("%d %d",&m,&n))
	{
		if(m == 0 && n == 0)
		{
			break;
		}
		for(i = 0 ; i < m ; i ++)//可能行列有?? 
		{
			scanf("%s",xx[i]);
		}
		sum = 0;
		memset(vis,-1,sizeof(vis));//可能会有??
		for(i = 0 ; i < m ; i ++)
		{
			for(j = 0 ; j < n ; j ++)
			{
				if(vis[i][j] == -1)
				{
					bfs(i,j,xx[i][j]);
					sum ++;
				}
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

Measured memory_bytes: 1108
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

int main(){
  int N;
  int OddCount = 0;
  bool ZeroFlag = false;
  cin >> N;
  vector<int> A;
  for(int i=0;i<N;i++){
    int a;
    cin >> a;
    if(a < 0){
      OddCount++;
    }
    if(a == 0){
      ZeroFlag = true;
    }
    A.push_back(a);
  }
  bool breakflag = false;

  long int ans = 0;
  while(!breakflag){
    if(OddCount % 2 == 0 || ZeroFlag){
        for(int i=0;i<A.size();i++){
	  ans += abs(A[i]);
	}
      break;
    }
    else{
      int min = abs(A[0]);
      int minE = 0;
      for(int i=0;i<A.size();i++){
	if(min > abs(A[i])){
	  min = abs(A[i]);
	  minE = i;
	}
      }
      for(int i=0;i<A.size();i++){
	 ans += abs(A[i]);
      }
      ans -= abs(A[minE]) * 2;
      break;
    }
  }

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


Measured memory_bytes: 892
import collections
import sys


def pow2(x, n):
    ret = 1
    while (n > 0):
        if (n & 1):
            ret *= x
        x *= x
        n = n >> 1
    return ret


n = int(input())
a = list(map(int, input().split()))

if 0 in a:
    print(0)
    sys.exit(0)

c = collections.Counter(a)

l = sorted(list(c.keys()))

ans = 1
for num in l:
    ans *= pow2(num, c[num])
    if ans > 1000000000000000000:
        print(-1)
        sys.exit(0)
print(ans)

Measured memory_bytes: 84764
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int H,W;
int ans;
char s[51][51];
int a[51][51];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
int bfs(){
  queue<int> q,q2;
  q.push(0);
  q2.push(0);
  a[0][0]=0;
  while(!q.empty()){
    int yy=q.front(),xx=q2.front();
    q.pop();q2.pop();
    for(int i=0;i<4;i++){
      int ny=yy+dy[i],nx=xx+dx[i];
      if(0<=nx && nx<W && 0<=ny && ny<H && s[ny][nx]=='.' && a[ny][nx]==-1){
	a[ny][nx]=a[yy][xx]+1;
	q.push(ny);q2.push(nx);
      }
    }
  }
  return a[H-1][W-1];
}
int main(){
  cin >> H >> W;
  ans=H*W-1;
  for(int i=0;i<H;i++)cin >> s[i];
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      a[i][j]=-1;
      if(s[i][j]=='#'){a[i][j]=0;ans--;}
    }
  }
  int x=bfs();
  if(x==-1)cout << "-1" << endl;
  else cout << ans-x << endl;

  
 
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <iomanip>
#include <queue>
#include <set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef unordered_map<int,int> m32;
typedef map<ll,ll> m64;
typedef unordered_map<ll,set<ll> > m648;
typedef pair<ll,p64 > pp64;
ll MOD = 1e9 + 7;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF LLONG_MAX
v64 dp;

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll h,w;
    cin >> h >> w;
    ll ans[h][w];
    forn(i,h) forn(j,w) ans[i][j]=0;
    ans[0][0]=1;
    char v;
    forn(i,h){
    	forn(j,w){
    		cin >> v;
    		if(v=='#') continue;
    		if(i-1>=0){
    			ans[i][j]+=ans[i-1][j];
    			ans[i][j]%=MOD;
    		}
    		if(j-1>=0){
    			ans[i][j]+=ans[i][j-1];
    			ans[i][j]%=MOD;
    		}
    	}
    }
    cout << ans[h-1][w-1] << ln;
    // forn(i,n+1) p[i]=0;
    // p[0]=1;
    // ld v;
    // forn(i,n){
    // 	cin >> v;
    // 	rforn(i,n){
    // 		if(i-1>=0){
    // 			p[i]=p[i-1]*v+p[i]*(1-v);
    // 		}
    // 		else{
    // 			p[i]=p[i]*(1-v);
    // 		}
    // 	}
    // 	// forn(i,n+1){
    // 	// 	cout << p[i] << " ";
    // 	// }
    // 	// cout << ln;
    // }
    // ld ans=0;
    // ll sh = (n+1)/2;
    // forn(i,(n+1)/2){
    // 	ans += p[sh+i];
    // }
    // cout << setprecision(30) << fixed;
    // // cout << std::fixed;
    // // cout << std::setprecision(30);
    // cout << ans << ln;
    return 0;
}

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

int main()
{
    int n, q, x;
    int cnt = 0;

    map<int, int> a;

    cin >> n;

    for( int i = 0; i < n; i++ )
    {
        cin >> q >> x;
        if( q == 0 )
        {   
            if( a[x] == 0 )
            {
                cnt++;
                a[x] = 1;
            }
            cout << cnt << endl;
        }
        else
        {    
            if( a[x] == 1 )
                cout << 1 << endl;
            else
                cout << 0 << endl;
        } 
    }
    return 0;
}


Measured memory_bytes: 12436
#include <iostream>

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

using namespace std;

int main(void){

    long long  S,C;

    cin >> S >> C;

    long long ans = 0;

    long long idealC = S*2;
    long long lackC = idealC-C;

    if(idealC <= C){
        C = C -idealC;
        ans = S;
    }else{
        C = 0;
        ans = S;
        ans = ans - ((lackC+1)/2);
    }
    
    ans += C / 4;

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        static final int SIZE = (int) 1e5 + 100;
        static boolean[] isPrime = new boolean[SIZE];
        static int[] similarCount = new int[SIZE];

        static {
            Arrays.fill(isPrime, true);
            isPrime[1] = false;
            for (int i = 2; i * i < SIZE; i++) {
                if (!isPrime[i]) continue;
                for (int j = 2, mul = j * i; mul < SIZE; j++, mul = j * i) {
                    isPrime[mul] = false;
                }
            }
            similarCount[0] = similarCount[1] = 0;
            for (int i = 3; i < SIZE; i += 2) {
                if (isPrime[i] && isPrime[(i + 1) / 2]) similarCount[i] = similarCount[i - 2] + 1;
                else similarCount[i] = similarCount[i - 2];
            }
        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int q = in.nextInt();
            for (int i = 0; i < q; i++) {
                int l = in.nextInt(), r = in.nextInt();
                out.println(similarCount[r] - similarCount[Math.max(0, l - 2)]);
            }
        }

    }
}



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

int main(void){
    int q;
    double p, x, y, n, m;

   while(scanf("%d", &q) != -1){
       if(q == -1) break;
       p = (double)q;
       m = 0.00001*p;
       x = p/2;
       y = x*x*x-p;
       n = fabs(y);
       
       do{
           x = x - (x*x*x-p)/(3*x*x);
           y = x*x*x-p;
           n = fabs(y);
       }while(n>=m);
       printf("%.6f\n", x);
   }
   
   return 0;
   
}



Measured memory_bytes: 2048
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
template <typename T,typename E>
struct SegmentTree{
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  int n;
  F f;
  G g;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(int n_,F f,G g,T ti):
    f(f),g(g),ti(ti){
    init(n_);
  }
  void init(int n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1,ti);
  }
  void build(int n_, vector<T> v){
    for(int i=0;i<n_;i++) dat[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--)
      dat[i]=f(dat[i*2+1],dat[i*2+2]);
  }
  void update(int k,E a){
    k+=n-1;
    dat[k]=g(dat[k],a);
    while(k>0){
      k=(k-1)/2;
      dat[k]=f(dat[k*2+1],dat[k*2+2]);
    }
  }
  inline T query(int a,int b){
    T vl=ti,vr=ti;
    for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[(l++)-1]);
      if(r&1) vr=f(dat[(--r)-1],vr);
    }
    return f(vl,vr);
  }

  int find(int a,int b,function<bool(T)> &check,int k,int l,int r){
    if(!check(dat[k])||r<=a||b<=l) return -1;
    if(k>=n-1) return k-(n-1);
    int m=(l+r)>>1;
    int vl=find(a,b,check,k*2+1,l,m);
    if(~vl) return vl;
    return find(a,b,check,k*2+2,m,r);
  }
  
  int find(int a,int b,function<bool(T)> &check){
    return find(a,b,check,0,0,n);
  }
  
};
//END CUT HERE
signed AOJ_DSL2A(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n,q;
  cin>>n>>q;
  SegmentTree<int,int> rmq(n,
			   [](int a,int b){return min(a,b);},
			   [](int a,int b){++a;return b;},
			   INT_MAX);
  for(int i=0;i<q;i++){
    int c,x,y;
    cin>>c>>x>>y;
    if(c) cout<<rmq.query(x,y+1)<<endl;
    else rmq.update(x,y);
  }
  return 0;
}
/*
  verified on 2018/03/04
  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_2_A&lang=jp
*/

signed ARC038_C(){
  Int n;
  cin>>n;
  vector<Int> c(n,0),a(n,0);
  for(Int i=1;i<n;i++) cin>>c[i]>>a[i];
  
  SegmentTree<Int, Int>::F f=[](Int a,Int b){return min(a,b);};
  SegmentTree<Int, Int>::G g=[](Int a,Int b){return max(a,b);};
  SegmentTree<Int, Int> seg(n,f,g,-1);
  
  vector<Int> dp(n);
  dp[0]=0;
  seg.update(0,0);
  for(Int i=1;i<n;i++){
    function<bool(Int)> check=[&](Int x){return x<i-c[i];};
    dp[i]=seg.find(0,n,check);
    seg.update(dp[i],i);
  }
  
  Int ans=0;
  for(Int i=1;i<n;i++)
    if(a[i]&1) ans^=dp[i];
  
  cout<<(ans?"First":"Second")<<endl;
  return 0;
}

/*
  verified on 2018/01/30
  https://beta.atcoder.jp/contests/arc038/tasks/arc038_c
*/

signed main(){
  AOJ_DSL2A();
  //ARC038_C();
  return 0;
}



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

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

    //一番間隔の広いところから始める
    long long distance = 0;
    for(int i = 0; i < n - 1; i++){
        distance = max(distance, a[i + 1] - a[i]);
    }
    distance = max(distance, k + a[0] - a[n - 1]);

    long long ans = k - distance;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <climits>
#include <bitset>

using namespace std;
typedef long long ll;

ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y){
    tmp=x;
    x=y;
    y=tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

//for(int i = ; i < ; i++){}

int main(){
  int x,a,b;
  cin>>x>>a>>b;
  cout<<(x-a)%b<<endl;
  return 0;
}




Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rep1(i, n) for(int i=1; i<=n; i++)
#define repr(i, n) for(int i=n-1; i>=0; i--)
#define repr1(i, n) for(int i=n; i>=1; i--)
#define all(v) v.begin(),v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
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 digit(ll x) {
    ll ret = 0;
    while (x != 0) {
        ret++;
        x /= 10;
    }
    return ret;
}

int main() {
    ll n; cin >> n;
    ll ans = LLINF;
    for (ll i = 1; i * i <= n; i++) {
        if (n % i == 0) chmin(ans, max(digit(i), digit(n / i)));
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
h, w = gets.split.map(&:to_i)

arr, ans = h.times.map{ gets.chomp.split("") }, h.times.map{ [-1] * w }

arr.each_index do |i|
  arr[i].each_index do |j|
    if arr[i][j] == "c"
      c = 0
      
      for k in j...w
        ans[i][k] = c
        c += 1
      end
    end
  end
end

ans.each do |elm|
  puts elm.join(" ")
end

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

int main() {
  int n;string s;cin>>n>>s;
  int ans=0;
  for(int i=1;i<n-1;i++){
    vector<int> a(26),b(26);
    for(int j=0;j<i;j++)a[s[j]-'a']=1;
    for(int j=i;j<n;j++)b[s[j]-'a']=1;
    int cnt=0;
    rep(j,26){
      if(a[j]==1&&b[j]==1)cnt++;
    }
    ans=max(ans,cnt);
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3656
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(x) x.begin(),x.end()
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

ll k;
ll solve(){
   if(k%2 == 0) return -1;
   ll res = 1;
   ll now = 7;
   int i = 0;
   for(;i < 1e8;i++){
       if(now%k==0) break;
       res++;
       now%=k;
       now*=10;
       now += 7;
   }
   if(i==1e8)return -1;
   return res;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>k;
    cout<<solve()<<endl;
    return 0;
}

Measured memory_bytes: 3636
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.math;

void main() {
    int n;
    long c;
    scan(n, c);
    auto x = new long[](n);
    auto v = new long[](n);
    iota(n).each!(i => scan(x[i], v[i]));

    long ans;
    ans = max(straight(n, c, x, v), straight(n, c, x.map!(xi => c - xi).retro.array, v.retro.array));
    debug {
        writeln(straight(n, c, x, v), ' ', straight(n, c, x.map!(xi => c - xi).retro.array, v.retro.array));
    }
    ans = max(ans, turn(n, c, x, v), turn(n, c, x.map!(xi => c - xi).retro.array, v.retro.array));
    debug {
        writeln(turn(n, c, x, v), ' ',  turn(n, c, x.map!(xi => c - xi).retro.array, v.retro.array));
    }


    writeln(ans);
}

long turn(int n, long c, long[] x, long[] v) {
    auto pm = new long[](n + 1);

    long s;
    foreach (i ; 1 .. n + 1) {
        s += v[i-1];
        pm[i] = max(pm[i-1], s - 2*x[i-1]);
    }

    long ans = pm[n];
    s = 0;
    foreach (i ; 1 .. n + 1) {
        s += v[n - i];
        ans = max(ans, pm[n - i] + s - (c - x[n - i]));
    }

    return ans;
}

long straight(int n, long c, long[] x, long[] v) {
    long ans;
    long cal;

    foreach (i ; 0 .. n) {
        cal += v[i];
        ans = max(ans, cal - x[i]);
    }

    return ans;
}




void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


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

using namespace std;

int main() {
  long long N, A[200001], ans{};
  cin >> N;
  for (int i = 0; i != N; ++i) {
    cin >> A[i];
  }
  sort(begin(A),begin(A)+N);
  for (int i = 1; i != N; ++i) {
    ans += A[N-i/2-1];
  }
  cout << ans << endl;
}


Measured memory_bytes: 5024
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <stack>
#include <list>
#include <map>
#include <unordered_map>
#include <chrono>
#include <numeric>
using namespace std;
using ll = long long;
const long long INF = (long long)1e18+1;
#define DIV 1000000007
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
#ifdef TEST
    chrono::system_clock::time_point start, end;
    start = chrono::system_clock::now();
#endif
    long idx=0;
    long N;
    cin >> N;
    bool can = false;
    for(size_t i=1;i<=9; i++)
    {
        if(N / i <= 9 && N % i == 0 ) can = true;
    }
    if(can) cout << "Yes" << endl;
    else cout << "No" << endl;
#ifdef TEST
    end = chrono::system_clock::now();
    cerr << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count() / 1000.0) << "[ms]" << endl;
#endif
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>    //C++の標準ライブラリのヘッダを全て読み込む
 
int main() {
    using std::cout;
    using std::endl;
    using std::cin;
    using std::string;

    int a,b,n;
    cin >> n >> a >> b;


    if (a*n < b) {
        cout << a*n << endl;
    } else {
        cout << b << endl;
    }

}

Measured memory_bytes: 256
#include <iostream>
#include <string>
using namespace std;
const int LEN = 1005;
string X,Y;
int dp[LEN][LEN];

int main()
{
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i)
	{
		cin >> X >> Y;

		int xn = X.size();
		int yn = Y.size();

		for (int j = 0; j <= xn; ++j)
		{
			dp[j][0] = 0;
		}

		for (int j = 0; j <= yn; ++j)
		{
			dp[0][j] = 0;
		}

		for (int j = 0; j < xn; ++j)
		{
			for (int k = 0; k < yn; ++k)
			{
				if (X[j] == Y[k])
				{
					dp[j+1][k+1] = max(dp[j][k]+1, max(dp[j][k+1], dp[j+1][k]));
				}
				else
				{
					dp[j+1][k+1] = max(dp[j][k+1], dp[j+1][k]);
				}
			}
		}

		cout << dp[xn][yn] << endl;
	}

	
	return 0;
}


Measured memory_bytes: 7100
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<algorithm>
#include<tuple>
#include<utility>
#include<cctype>
#include<climits>
#include<map>
#include<queue>
#include<functional>
#include<stack>
#include<iomanip>

using namespace std;

#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPL(i,a,n) for(ll i=a;i<n;++i)
#define RUP(a,b) ((a+b-1)/(b))
#define ENT "\n"
#define SRTVG(v) sort(v.begin(),v.end(),greater<>())
#define SRTV(v) sort(v.begin(),v.end())
#define SRTAG(a,n) sort(a,a+n,greater<>())
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define INF LLONG_MAX/2

typedef long long ll;
typedef tuple<int,int,bool> Tb;
typedef pair<int,int> Pii;
typedef vector<int> Vi;

template<class T> void chmax(T& a, T b) {if(a < b){a=b;}}
template<class T> void chmin(T& a, T b) {if(a > b){a=b;}}
template<class T> void YesNo(T& a) {if(a){cout << "Yes" << ENT;}else{cout << "No" << ENT;}}
template<class T> void YESNO(T& a) {if(a){cout << "YES" << ENT;}else{cout << "NO" << ENT;}}

int atcoder(){
    ll n,k;
    cin>>n>>k;
    vector< vector<ll> > v = vector< vector<ll> > (8,vector<ll>(n,0));;
    REP(i,0,n){
        ll x,y,z;
        cin>>x>>y>>z;
        REP(j,0,8){
            if(j<4) v[j][i]+=x;
            else v[j][i]-=x;
            if((j/2)%2==0) v[j][i]+=y;
            else v[j][i]-=y;
            if(j%2==0) v[j][i]+=z;
            else v[j][i]-=z;
        }
    }
    REP(i,0,8) SRTVG(v[i]);
    ll ans=0;
    REP(i,0,8){
        ll tmp=0;
        REP(j,0,k){
            tmp += v[i][j];
        }
        chmax(ans,tmp);
    }
    cout<<ans<<ENT;
    return 0;
}


signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    atcoder();
    return 0;
}




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

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <complex>
#include <string>
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <assert.h>

using namespace std;
#define el  '\n'
#define ll long long
#define clr(a, b)memset(a,b,sizeof(a))
#define sz(a) (int)(a).size()
const long long N = 102, OO = 0x3f3f3f3f, MOD = (int) 1e9 + 7;

ll dp[2][(int) 1e5 + 2];

class DKnapsack1 {
public:
    void init() {
        clr(dp,0);
    }

    void solve(istream &cin, ostream &cout) {
//        init();
        int n, totW;
        cin >> n >> totW;
        bool first = true;
        for (int j = 0; j < n; j++) {
            int w, v;
            cin >> w >> v;
            first = true;
            for (int curW = 0; curW <= totW; curW++) {
                int i = j%2;
                if (first) {
                    first = false;
                    dp[i % 2][curW] = (curW - w >= 0) ? v : 0;
                    continue;
                }
                if (curW - w >= 0)
                    dp[i][curW] = max(dp[(((i - 1) % 2 + 2) % 2)][curW], dp[(((i - 1) % 2 + 2) % 2)][curW - w] + v);
                else
                    dp[i][curW] = dp[(((i - 1) % 2 + 2) % 2)][curW];
            }
        }

        cout << dp[(((n - 1) % 2 + 2) % 2)][totW] << el;
    }
};
int main() {
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	DKnapsack1 solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


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

public class Main implements Runnable {
	public static void main (String[] args) {new Thread(null, new Main(), "_cf", 1 << 28).start();}

	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("");

		int n = fs.nextInt();
		int k = fs.nextInt();
		int[] a = fs.nextIntArray(n);
		int val = 0;
		for(int i : a) val += i;
		int res = 1;
		int[] list = new int[n];
		int[] sum = new int[n];
		for(int gcd = 1; gcd*gcd <= val; gcd++) {
			if(val%gcd != 0) continue;
			int[] vs = {gcd, val/gcd};
			for(int j = 0; j < 2; j++) {
				for(int i = 0; i < n; i++) {
					list[i] = a[i]%vs[j];
				}
				Arrays.sort(list);
				for(int i = n-1; i >= 0; i--) {
					sum[i] = vs[j]-list[i];
					if(sum[i] == vs[j]) sum[i] = 0;
					if(i+1 < n) sum[i] += sum[i+1];
				}
				int cur = 0;
				int moves = -1;
				for(int i = 0; i < n-1; i++) {
					cur += list[i];
					if(cur == sum[i+1]) {
						moves = cur;
					}
				}
				if(moves != -1 && moves <= k) {
					res = Math.max(res, vs[j]);
				}
			}
		}
		out.println(res);
			
		out.close();
	}

	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) {
			try {
				in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
			}
			catch (Exception e) {
				in = new BufferedInputStream(System.in, BS);
			}
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

}

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

signed main()
{
  string s,f,b;
  cin >> s;
  int n = s.size();
  string t,a,c;
  t = s;
  reverse( t.begin() , t.end() );
  if( t!=s ) return cout << "No" , 0;
  f = s.substr( 0, (n-1)/2 );
  a = f;
  reverse( a.begin() , a.end() );
  if( a!=f ) return cout << "No" , 0;
  b = s.substr( (n+3)/2-1 , n- (n+3)/2+1 );
  c = b;
  reverse( c.begin(), c.end() );
  if( b != c ) return cout << "No", 0;
  cout << "Yes\n";
  
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define Repr(i, n, a) for (int i = (n); i >= (a); i--)
#define rep(i, n) Rep(i, 0, n)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

int n;
vector<int> a, sum;
int dp[410][410];
bool vis[410][410];

int solve(int N, int M)
{
    if (vis[N][M])
        return dp[N][M];
    vis[N][M] = true;
    if (N == M)
        return dp[N][M] = 0;
    int res = INF;
    for (int i = N; i < M; i++)
        res = min(res, solve(N, i) + solve(i + 1, M) + sum[i + 1] - sum[N] + sum[M + 1] - sum[i + 1]);
    return dp[N][M] = res;
}

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

    cin >> n;
    a.resize(n);
    rep(i, n) cin >> a[i];
    sum.resize(n + 1);
    rep(i, n) sum[i + 1] = sum[i] + a[i];
    cout << solve(0, n - 1) << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<int> front(N - 1), back(N - 1);
  int gcdf = A[0];
  for (int i = 0; i < front.size(); i++) {
    gcdf = __gcd(gcdf, A[i]);
    front[i] = gcdf;
  }

  reverse(A.begin(), A.end());

  int gcdb = A[0];
  for (int i = 0; i < back.size(); i++) {
    gcdb = __gcd(gcdb, A[i]);
    back[i] = gcdb;
  }

  reverse(back.begin(), back.end());

  vector<int> gcd(N);
  for (int i = 0; i < N; i++) {
    if (i == 0) {
      gcd[i] = back[0];
    } else if (i == N - 1) {
      gcd[i] = front[front.size() - 1];
    } else {
      gcd[i] = __gcd(front[i - 1], back[i]);
    }

    // cout << gcd[i] << endl;
  }

  sort(gcd.begin(), gcd.end());
  reverse(gcd.begin(), gcd.end());
  cout << gcd[0] << endl;
};


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
#define ll long long 

int N, W;
int arr[200][2];
ll dp[110][1000000]; // dp[N][W]

int main()
{
    cin >> N >> W;
    for(int i = 0; i < N; i++)
    {
        // get weight  >> value
        cin >> arr[i+1][0] >> arr[i+1][1] ;
    }

    for(int i = 0; i <= N; i++)
    {
        for(int j = 0; j <= W; j++)
        {
            if(i == 0 || j == 0) dp[i][j] = 0;
            else if(arr[i][0] <= j)
            {
                dp[i][j] = max(dp[i-1][j-arr[i][0]] + arr[i][1], dp[i-1][j]);
            }
            else
            {
                dp[i][j] = dp[i-1][j];
            }
            
        }
    }

    printf("%lld", dp[N][W]);
    return 0;
}

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

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

  switch(n % 10) {
    case 2:
    case 4:
    case 5:
    case 7:
    case 9: cout << "hon" << endl;
            break;
    case 0:
    case 1:
    case 6:
    case 8: cout << "pon" << endl;
            break;
    case 3: cout << "bon" << endl;
            break;
  }
  return 0;
}

Measured memory_bytes: 3648
#![allow(unused_imports)]

use text_io::*;
use proconio::*;
use std::collections::*;
use std::process::exit;
use std::cmp::*;
use itertools::Itertools;


fn main() {
    input! {
    a:usize,
    b:usize,
    c:String,
    }
    let mut ans=0;
    let c=c.chars().map(|a|a.to_digit(10).unwrap_or(100)).collect_vec();
    if c[a]==100 {ans+=10000}
    for i in 0..c.len() {
        if c[i]==100 {ans+=1}
    }
    println!("{}",if ans==10001{"Yes"}else{"No"})
}

Measured memory_bytes: 2152
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
  String s = sc.next();
  String t = sc.next();
  char[] a = s.toCharArray();
  char[] b = t.toCharArray();
  int p = -1;
  boolean ex = false;
  for(int i = a.length - b.length;i >= 0;i--){
    boolean ok = true;
    for(int j = i;j < i + b.length;j++){
      if(a[j] != b[j-i] && a[j] != '?'){
        ok = false;
        break;
      }
    }
    if(ok){
      p = i;
      ex = true;
      break;
    }
  }
  if(ex){
    for(int i = 0;i < p;i++){
      System.out.print(a[i] == '?' ? 'a':a[i]);
    }
    if(p + b.length == a.length){
    System.out.println(t);
    }
    else{
      System.out.print(t);
      for(int i = p + b.length;i < a.length;i++){
        System.out.print(a[i] == '?' ? 'a':a[i]);
      }
      System.out.println("");
    }
  }
  else{
    System.out.println("UNRESTORABLE");
  }
}}

Measured memory_bytes: 21844