akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <cstdio>
 
using namespace std;
using ll = long long;
 
#define rep(i, a) for (int i = 0; (i) < (int) (a); (i)++)
#define reps(i, a, b) for (int i = (int) (a); (i) < (int) (b); (i)++)
#define rrep(i, a) for (int i = (int) a-1; (i) >= 0; (i)--)
#define rreps(i, a, b) for (int i = (int) (a)-1; (i) >= (int) (b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define RS resize
#define CINV(v, N) do {\
	v.RS(N);\
	rep(i, N) cin >> v[i];\
} while (0);
#define RCINV(v, N) do {\
	v.RS(N);\
	rrep(i, N) cin >> v[i];\
} while (0);
 
#define MOD 1000000007
void init();
void solve();

signed main()
{
    init();
    solve();
}


vector<ll> a, c, hl, hr;
vector<ll> pt(100100);
string s;
int l;
ll K;

ll modpow(ll a=9LL, int b=1000000005)
{
	if (b == 0) return 1;
	if (b % 2) {
		return modpow(a, b-1) * a % MOD;
	}
	ll t = modpow(a, b / 2);
	return t * t % MOD;
}

void init()
{
	cin >> s;
	l = s.length();
	a.RS(l, 0);
	c.RS(l, 0);
	hl.RS(l, 0);
	hr.RS(l, 0);
	reps(i, 1, l) {
		a[i] = a[i-1] + (s[i-1] == 'A');
		hl[i] = hl[i-1] + (s[i-1] == '?');
	}
	rrep(i, l-1) {
		c[i] = c[i+1] + (s[i+1] == 'C');
		hr[i] = hr[i+1] + (s[i+1] == '?');
	}
	pt[0] = 1;
	reps(i, 1, 100100) {
		pt[i] = pt[i-1] * 3 % MOD;
	}
	K = modpow();
}

void solve()
{
	ll res = 0;
	rep(i, l) {
		if (s[i] == 'B' || s[i] == '?') {
			ll x = 3 * a[i] + hl[i];
			ll y = 3 * c[i] + hr[i];
			x %= MOD;
			y %= MOD;
			ll H = hl[i] + hr[i];
			ll t = x * y % MOD;
			res += t * pt[H] % MOD * K % MOD;
			res %= MOD;
		}
	}
	cout << res << endl;
}



Measured memory_bytes: 4352
def main():
    N,Y = map(int, input().split())

    for x in range(N+1):
        for y in range(N-x+1):
            z = Y - 10000 * x - 5000 * y
            z //= 1000
            if z == N - x - y:
                print(x, y, z)
                exit()

    print("-1 -1 -1")


if __name__ == "__main__":
    main()


Measured memory_bytes: 3060
#define _USE_MATH_DEFINES

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <limits>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <bitset>

using namespace std;

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

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    vector<P> vp;
    for (auto &c : s) {
        if (vp.empty() || vp.back().first != c) {
            vp.emplace_back(c, 1);
        }else{
            vp.back().second++;
        }
    }

    int ans = 0, len = 0, sum = 0, r = 0;
    int m = (int)vp.size();
    for (int l = 0; l < m; ++l) {
        while(r < m && sum + (vp[r].first == '0') <= k){
            sum += vp[r].first == '0';
            len += vp[r].second;
            ++r;
        }
        ans = max(ans, len);

        sum -= (vp[l].first == '0');
        len -= vp[l].second;
    }
    cout << ans << endl;
}

Measured memory_bytes: 1528
#include<bits/stdc++.h>
using namespace std;
int n,k,a[100003];
int main(){cin>>n>>k;
	for(int i=0;i<n;i++)
		scanf("%d",a+i);
	int mxm=0,j=-1;
	for(int i=1;i<n;i++)
		if(a[i-1]+a[i]>mxm){
			mxm=a[i-1]+a[i];
			j=i;
		}
	if(mxm<k)
		cout<<"Impossible";
	else{
		cout<<"Possible";
		for(int i=1;i<j;i++)
			cout<<endl<<i;
		for(int i=n-1;i>=j;i--)
			cout<<endl<<i;
	}
}

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

int main(){
  int n;
  cin >> n;
  int x[n],y[n];
  for(int i = 0;i < n;i++){
    cin >> x[i] >> y[i];
  }
  int dx[n][n],dy[n][n];
  for(int i = 0;i < n;i++){
    for(int j = i + 1;j < n;j++){
      dx[i][j] = x[i] - x[j];
      dy[i][j] = y[i] - y[j];
    }
  }
  int result = 100000;
  
  for(int i = 0;i < n;i++){
    for(int j = i + 1;j < n;j++){
      int c = 0;
      for(int k = 0;k < n;k++){
        for(int l = k + 1;l < n;l++){
          if((dx[i][j] == dx[k][l] & dy[i][j] == dy[k][l]) || (dx[i][j] == -dx[k][l] & dy[i][j] == -dy[k][l])){
            c++;
          }
        }
      }
      result = min(result,n-c);
    }
  }
  if(n == 1){
    cout << 1 << endl;
  }
  else{
    cout << result << endl;
  }
}

Measured memory_bytes: 256
n,k = gets.chomp.split(' ').map(&:to_i)
sunukes = Array.new(n,false)
k.times do
  gets
  gets.chomp.split(' ').map(&:to_i).each do |j|
    sunukes[j-1] = true
  end
end
puts sunukes.count(false)

Measured memory_bytes: 14428
#include<bits/stdc++.h>
#define pb push_back
#define rep(a,b,c) for(int a=(int)b;a<(int)c;a++)
#define repk(a,b,c,k) for(int a=(int)b;a<(int)c;a+=(int)k)
#define comeback std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int,int>
#define ful(a) a.begin(),a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x,int n)
{
    if(n==0)
    return 1;
    else if(n%2==0)
    return em((x%mod*x%mod)%mod,n/2);
    else
    return (x%mod*em((x%mod*x%mod)%mod,(n-1)/2))%mod;
}
signed main()
{
	comeback
	/*#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output1.txt","w",stdout);
	#endif*/
         int n,W,i,j,k,l;
         cin>>n>>W;
         int w[n+1],va[n+1];
         for(i=1;i<=n;i++){
         	cin>>w[i]>>va[i];
         }
         int dp[n+1][W+1];
         memset(dp,-1,sizeof(dp));
         for(i=0;i<=W;i++)
         	dp[0][i]=0;
         for(i=1;i<=n;i++){
         	for(j=0;j<=W;j++){
         		if(w[i]<=j)
         			dp[i][j]=max(va[i]+dp[i-1][j-w[i]],dp[i-1][j]);
         		else
         			dp[i][j]=dp[i-1][j];
              	}
         }
         cout<<dp[n][W];
}

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

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using i32 = int_fast32_t;
using u32 = uint_fast32_t;
using i64 = int_fast64_t;
using u64 = uint_fast64_t;

namespace chrono = std::chrono;
using namespace std::string_literals;

#define all(c) (c).begin(), (c).end()

__attribute__((constructor))
void fast_iostream() {
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cerr.tie(nullptr);
}

template <class Graph>
bool dfs1(const Graph& g, u32 v, i32 cur_type, std::vector<i32>& t) {
  t[v] = cur_type;
  bool flag = false;
  for (u32 next: g[v]) {
    if (t[next] == -1) {
      flag |= dfs1(g, next, cur_type ^ 1, t);
    } else if (t[v] == t[next]) {
      flag |= true;
    }
  }
  return flag;
}

int main() {
  u32 n, m;
  cin >> n >> m;
  std::vector<std::vector<u32>> g(n);
  for (u32 i = 0; i < m; ++i) {
    u32 x, y;
    cin >> x >> y;
    --x; --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  std::vector<i32> t(n, -1);
  bool has_cycles = dfs1(g, 0, 0, t);

  i64 k0 = 0;
  i64 k1 = 0;
  for (i32 ct: t) {
    if (ct == 1) {
      k1 += 1;
    } else {
      k0 += 1;
    }
  }
  if (!has_cycles) {
    cout << k1 * k0 - m << endl;
  } else {
    cout << n * (n - 1) / 2 - m << endl;
  }
}


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


int Partition(int A[],int p,int r){
  int x = A[r];
  int i = p-1;
  for(int j=p;j<r;j++){
    if(A[j] <= x){
      i = i+1;
      int h=A[i];
      A[i] = A[j];
      A[j] = h;
    }
  }
  int h = A[i+1];
  A[i+1] = A[r];
  A[r] = h;



  return i+1;
}


int main(){
  int n,index;
  while(scanf("%d",&n) != EOF){
    int *A = new int[n+1];
    for(int i=1;i<=n;i++){
      scanf("%d",&A[i]);
    }
    index = Partition(A,1,n);
    for(int i=1;i<=n;i++){
      if(index == i)printf("[");
      printf("%d",A[i]);
      if(index == i)printf("]");
      if(i != n)printf(" ");
    }
    printf("\n");
    delete [] A;
  }
  return 0;
}

Measured memory_bytes: 0
#include <stdio.h>
int main(void){
int a,b;
char s[100];
  char ss[100];
  scanf("%s",s);
    scanf("%s",ss);
if((s[1]==ss[1])&&(s[0]==ss[2])&&(s[2]==ss[0])){printf("YES");}
  else{printf("NO");}
}

Measured memory_bytes: 128
#include <bits/stdc++.h> 
#include <ext/pb_ds/assoc_container.hpp> 
using namespace std;
using namespace __gnu_pbds;
// Policy based data structure 
typedef tree<int, null_type, 
			less_equal<int>, rb_tree_tag, 
			tree_order_statistics_node_update> 
	indexed_set;  
#define ll long long int
#define pii pair<ll,ll>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define vi vector<ll>
#define vii vector<pii>
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define yes cout<<"YES"<<endl; return;
#define no cout<<"NO"<<endl; return;
#define flus fflush(stdin); fflush(stdout);
#define F first
#define S second
#define np next_permutation
#define inf 1e18
#define mod 1000000007
#define N 1000009
#define pi (double)2*acos(0.0)
#define minpq priority_queue <ll, vector<ll>, greater<ll>>
#define maxpq priority_queue<ll> 
void sout(){
	cout<<endl;
}
template <typename T,typename... Types>
void sout(T var1,Types... var2){
	cout<<var1<<" ";
	sout(var2...);
}
/*******************************************************************/

ll prime[N];
void solve(){
	memset(prime,0,sizeof prime);
	ll n;
	cin>>n;
	ll a[n];
	rep(i,0,n)
		cin>>a[i];
	ll g=0;
	rep(i,0,n){
		g=__gcd(g,a[i]);
	}
	if(g!=1){
		cout<<"not coprime"<<endl;
		return;
	}
	prime[1]=1;
	rep(i,2,N){
		if(prime[i]!=0)
			continue;
		for(ll j=i;j<N;j+=i){
			if(j>=N or j<0)
				continue;
			if(prime[j]!=0)
				continue;
			prime[j]=i;
		}
	}
	// rep(i,0,n){
	// 	cout<<a[i]<<" ";
	// }
	// cout<<endl;
	map<int,bool> vis;
	rep(i,0,n){
		ll temp=a[i];
		while(temp!=1){
			if(vis[prime[temp]]){
				cout<<"setwise coprime"<<endl;
				return;
			}
			vis[prime[temp]]=true;
			ll x=prime[temp];
			while(temp%x==0){
				temp/=x;
			}
		}
	}
	cout<<"pairwise coprime"<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}

Measured memory_bytes: 19240
def solve():
    import sys
    input = sys.stdin.readline

    N, M = map(int, input().split())
    adjL = [[] for _ in range(N)]
    adjLRev = [[] for _ in range(N)]
    outdegs = [0] * N
    for _ in range(M):
        s, t = map(int, input().split())
        s, t = s-1, t-1
        adjL[s].append(t)
        adjLRev[t].append(s)
        outdegs[s] += 1

    probs = [0] * N
    expes = [0] * N
    probs[0] = 1
    for v in range(N):
        prob, expe, outdeg = probs[v], expes[v], outdegs[v]
        if prob != 0:
            num = expe / prob
            for v2 in adjL[v]:
                probs[v2] += prob / outdeg
                expes[v2] += prob * (num+1) / outdeg

    probRevs = [0] * N
    expeRevs = [0] * N
    probRevs[-1] = 1
    for v in reversed(range(N)):
        prob, expe = probRevs[v], expeRevs[v]
        if prob != 0:
            num = expe / prob
            for v0 in adjLRev[v]:
                probRevs[v0] += prob / outdegs[v0]
                expeRevs[v0] += prob * (num+1) / outdegs[v0]

    ans = expes[-1]
    for vRem in range(N-1):
        if outdegs[vRem] == 1: continue
        values = []
        prob, expe, outdeg = probs[vRem], expes[vRem], outdegs[vRem]
        for v2 in adjL[vRem]:
            value = expe*probRevs[v2] + expeRevs[v2]*prob + prob*probRevs[v2]
            values.append(value)
        sumV = sum(values)
        ans2 = expes[-1] - sumV / outdeg
        for value in values:
            ans = min(ans, ans2 + (sumV-value) / (outdeg-1))

    print(ans)


solve()


Measured memory_bytes: 13044
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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; }

const ll INF=(1LL<<60);
const int mod=1000000007;

int main(){
    int a,b,k; cin >> a >> b >> k;
    int id=0;


    for(int i=min(a,b); i>=1; i--){
        if(a%i==0 && b%i==0){
            id++;
            if(id==k){
                cout << i << endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 256
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        Map<Long, Long> map = new TreeMap<>();
        long sum = 0;
        long max = 0;
        for (int i = 0; i < n; i++) {
            long a = Long.parseLong(sc.next());
            long b = Long.parseLong(sc.next());
            sum += a;
            max = Math.max(max, b);
            map.merge(b, a, Long::sum);
        }
        if (max < sum) {
            System.out.println("No");
            return;
        }
        sum = 0;
        for (Map.Entry<Long, Long> e : map.entrySet()) {
            sum += e.getValue();
            if (sum > e.getKey()) {
                System.out.println("No");
                return;
            }
        }
        System.out.println("Yes");
    }
}

Measured memory_bytes: 89428
<?php
	$str = trim(fgets(STDIN));
	$q = trim(fgets(STDIN));
	for ($i = 0; $i < $q; $i++) {
		$c = explode(" ", trim(fgets(STDIN)));
		$n = $c[2] - $c[1] + 1;
		if ($c[0] == "replace") {
			$str = substr_replace($str, $c[3], $c[1], $n);
		} elseif ($c[0] == "reverse") {
			$str = substr_replace($str, strrev(substr($str, $c[1], $n)), $c[1], $n);
		} elseif ($c[0] == "print") {
			echo substr($str, $c[1], $n) . "\n";
		}
	}

Measured memory_bytes: 6620
n,k = map(int,input().split())
a = list(map(int,input().split()))
m = max(k,max(a))
b = 1
count = 0
while m > b:
    b *= 2
    count += 1
p = 2**(count-1)
k2 = 0
ans = 0
for i in range(count):
    count1 = 0
    for j in range(n):
        if a[j] // p == 1:
            a[j] -= p
            count1 += 1
    if count1 >= n - count1:
        ans += count1*p
    else:
        if k2 + p <= k:
            ans += (n-count1)*p
            k2 += p
        else:
            ans += count1*p
    p = p//2
print(ans)

Measured memory_bytes: 14836
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
 
using namespace std;
 
#define PB push_back
#define MP make_pairA
#define REP(i,n) for(int i=0;i<(n);i++)
int main()
{
    int H,W,h,w;
    cin>>H>>W>>h>>w;
    cout<<H*W-h*W-H*w+h*w<<endl;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int dh[]={-1,0,1,0},dw[]={0,1,0,-1},h,w;
char s[105][105];
void dfs(int _h,int _w,char ch){
	s[_h][_w]='.';
	for(int i=0;i<4;i++){
		int H=_h+dh[i],W=_w+dw[i];
		if(H>=0&&H<h&&W>=0&&W<w&&s[H][W]==ch)dfs(H,W,ch);
	}
}
int main(){
	while(cin>>h>>w,h){
		int count=0;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>s[i][j];
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)if(s[i][j]!='.')count++,dfs(i,j,s[i][j]);
		cout<<count<<endl;
	}
}

Measured memory_bytes: 1640
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main(int argc, char *argv[]) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);//incompatible with `while (cin >> ...)'
    int a,b;
    cin >> a >> b;
    cout << a * b - a - b + 1 << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong p) {
	int i=0;
	for(int j = 0; j < k; j++ ) {
		llong sum = 0;
		while( sum + T[i] <= p) {
			sum += T[i];
			i++;
			if(i == n ) return n;
		}
	}
	return i;
}

int solve() {
	llong left = 0;
	llong right = 100000 * 10000;
	llong mid;
	while(left < right) {
		mid = (left + right) / 2;
		int v = check(mid);
		if(v == n) right = mid;
		else left = mid +1;
	}
	return right;
}

main() {
	cin >> n >> k;
	for( int i = 0; i < n; i++) cin >> T[i];
	llong ans = solve();
	cout << ans << endl;
}

Measured memory_bytes: 3860
import java.util.*;
import java.math.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] a = new long[n+1];
    long[] b = new long[n];

    for (int i = 0; i < n+1; i++) {
      a[i] = sc.nextInt();
    }

    for (int i = 0; i < n; i++) {
      b[i] = sc.nextInt();
    }

    long ans = 0;

    for (int i = 0; i < n; i++) {
      if (a[i] <= b[i]) {
        ans += a[i];
        b[i] -= a[i];
        a[i] = 0;
        if (a[i+1] <= b[i]) {
          ans += a[i+1];
          b[i] -= a[i+1];
          a[i+1] = 0;
        } else {
          ans += b[i];
          a[i+1] -= b[i];
        }
      } else {
        ans += b[i];
      }
    }

    System.out.println(ans);

  }

}


Measured memory_bytes: 82488
import copy

def Warshall_Floyd(edges,N):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        edges[i][j]=min(edges[i][j],edges[i][k]+edges[k][j])
  return edges

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
g=[[float('inf')]*n for _ in range(n)]
for i in range(n):
  for j in range(n):
    g[i][j]=arr[i][j]
d=Warshall_Floyd(copy.deepcopy(g),n)
ans=0
for a in range(n):
  for b in range(n):
    if a>=b:
      continue
    w=g[a][b]
    if d[a][b]<w:
      print(-1)
      exit()
    else:
      tmp=10**10
      for k in range(n):
        if k==a or k==b:
          continue
        tmp=min(tmp,d[a][k]+d[k][b])
      if w==tmp:
        continue
      else:
        ans+=w
else:
  print(ans)


Measured memory_bytes: 60380
import java.util.*;
public class Main {
	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		char [] s = sc.next().toCharArray();
		int x = 0;
		int max = x;
		
		for(int i = 0; i < n; i++){
		switch(s[i]){
		case 'I': x += 1; break;
		case 'D': x -= 1; break;
		default : 
		}
		if(max < x){ max = x; }
		}
		System.out.println(max);
	}
}


Measured memory_bytes: 9680
//BISMILLAH
#include <bits/stdc++.h>
using namespace std;
#define sz 109

    long long int solve(long long int i,long long int w,vector <vector<long long int> > &dp,vector<long long int> &weight,vector<long long int> &profit,long long int nn){
        if(i == nn || w <= 0) return 0;
        if(dp[i][w] != -1) return dp[i][w];
        long long int x = 0,y = 0;
        if(w - weight[i] >= 0){
              x = profit[i] + solve(i + 1,w - weight[i],dp,weight,profit,nn);
        }
         y = solve(i+1,w,dp,weight,profit,nn);
         return dp[i][w] = max(x,y);
    }
int main(){
     
    long long int w,v,i,j,n;
    
    cin >> n >> w;
    vector <long long int> weight(n);
    vector <long long int> profit(n);
    vector <vector<long long int> > dp(sz,vector<long long int>(w + 1,-1));
    for(i = 0;i<n;i++) cin >> weight[i] >> profit[i];
    
    cout << solve(0,w,dp,weight,profit,n) << endl;
    
    return 0;
}

Measured memory_bytes: 89588
#include<stdio.h>

int main(){
	int n,x[110],a,i,j,f,z;
	while(scanf("%d",&n)!=EOF){
		if(n==0)break;f=n;a=0;
		for(i=0;i<n;i++)scanf("%d",&x[i]);
		while(f>1){
			i=0;j=n-1;
			while(i!=j){
				if(x[i]>x[i+1]){
					z=x[i],x[i]=x[i+1],x[i+1]=z,a++;
				}else{
					i++;
				}
			}
			f--;
		}
		printf("%d\n",a);
	}
	return 0;
}

Measured memory_bytes: 0
import sys

def solve():
    file_input = sys.stdin
    N, W = map(int, file_input.readline().split())
    C = [0] * (W + 1)
    for line in file_input:
        item_v, item_w = map(int, line.split())
        for i, vals in enumerate(zip(C[item_w:], C)):
            v1, v2 = vals
            v1 += item_v
            if v1 > v2:
                C[i] = v1
    print(C[0])

solve()


Measured memory_bytes: 6484
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
//const ll INF=1018LL+5;
const ll MOD=1e9+7;
template<typename T> void max_self(T& a, T b) {//template of anything
    a = max(a, b);
}


void min_self(ll& a, ll b) {
    a = min(a, b);
}

//G -Longest Path
int Grid()
{
  int n;cin>>n;
   int m;cin>>m;
   vector<vector<char > >grid(n+1,vector<char>(m+1));
   for(int i=0;i<n;i++)
   {
    for(int j=0;j<m;j++)
        cin>> grid[i][j];
   }

   vector<vector<int  > >dp(n+1,vector<int>(m+1));
    for(int i=0;i<m;i++)
    {
        if(grid[0][i]!='#')
            dp[0][i]=1;
        else break;

    }
    for(int i=0;i<n+1;i++)
    {
        if(grid[i][0]!='#')
            dp[i][0]=1;
        else break;

    }
    for(int i=1;i<n;i++)
    {
        for(int j=1;j<m;j++)
            {if(grid[i][j] !='#')
             dp[i][j]=(dp[i-1][j]+dp[i][j-1])%MOD;
             else
               dp[i][j]=0;
            }
    }
    cout<<dp[n-1][m-1]%MOD;
  // int answer=0;
   //for(int i=1;i<=n;i++)
   // answer=max(answer,dist[i]);
   //cout<<answer;


}
int32_t main() {
  Grid();
    return 0;
}


Measured memory_bytes: 9088
#!/usr/bin/env ruby
x, n = gets.chomp.split(" ").map(&:to_i)
p = gets.chomp.split(" ").map(&:to_i)

p = p.sort

ans = 0

if p.empty?
  puts x
else
  0.upto(101) do |i|
    next if p.include?(i)

    if (x - i).abs < (x - ans).abs
      ans = i
    elsif (x - i).abs == (x - ans).abs && i < ans
      ans = i
    end
  end
  puts ans
end



Measured memory_bytes: 14244
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int a,b;
    cin >> a >> b;

    int x,y;
    double z;


    x = a / b;

    y = a % b;

    cout << fixed << setprecision(5);

    double d = (double)b;

    z = a / d;

    cout << x << " " << y << " " << z << endl;
} 


Measured memory_bytes: 3264
/*
Looking from a window above
It's like a story of love
Can you hear me?
Came back only yesterday
I'm moving further away
Want you near me
All I needed was the love you gave
All I needed for another day
And all I ever knew
Only you
Sometimes when I think of her name
When it's only a game
And I need you
Listen to the words that you say
It's getting harder to stay
When I see you
All I needed was the love you gave
All I needed for another day
And all I ever knew
Only you, only you
Uh uh uh only you, only you
Only you, only you, only you
This is going to take a long time
And I wonder what's mine
Can't take no more
Wonder if you'll understand
It's just the touch of your hand
Behind a closed door
And all I needed was the love you gave
All I needed for another day
And all I ever knew
Only you, only you, only you
All I needed was the love you gave
All I needed for another day
And all I ever knew
Only you, only you, only you
Only you
*/

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define ld long double
#define endl '\n'
#define rep(x,s,e) for(int x=s;x<=(e);x++)
#define repn for(int x=0;x<n;x++)
#define rep1n for(int x=1;x<=n;x++)
#define rep1m for(int y=1;y<=m;y++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define precise(x) cout << fixed << setprecision(x)
#define bits(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) ((int)(x.size()))
#define test int t; cin>>t; while(t--)
#define FAST                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)

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


void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}


template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define see(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define see(x...)
#endif

const int inf = 3e18;
const int N = 5e5 + 100;




int32_t main(){ 
   FAST;
   int n, a ,b;
   cin >> n >> a >> b;
   cout << min(n*a, b);
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>

#include <string.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }


//---------- 最大公約数 ----------
int gcd(int x, int y)
{
	int r;
	while ((r = x % y) != 0)
	{
		x = y;
		y = r;
	}
	return y;
}


//----------最小公倍数----------
int lcm(int x, int y)
{
	return (x / gcd(x, y) * y);
}



int N, M;
int A[100001];

signed main()
{
	cin >> N >> M;
	REP(i, N) cin >> A[i];

	int cur = 1;
	for (int i = 0; i < N; i++)
	{
		cur = lcm(cur, A[i]);
	}

	int ans = (2 * M) / cur;
	if (ans == 0)
	{
		ans = 0;
	}
	else if (ans % 2 == 0)
	{
		ans = ans / 2;
	}
	else
	{
		ans = ans / 2 + 1;
	}

	int target = cur / 2;
	for (int i = 0; i < N; i++)
	{
		int a = (target - A[i] / 2);

		if(a % A[i] != 0)
		{
			cout << 0 << endl;
			return 0;
		}
	}

	cout << ans << endl;
}



Measured memory_bytes: 3980
#include <iostream>
using namespace std;
int main()
{
    int A, B;
    cin >> A >> B;
    if(A != 1 && B != 1) {
        cout << 1 << endl;
    } else if(A != 2 && B != 2) {
        cout << 2 << endl;
    } else {
        cout << 3 << endl;
    }
    return 0;
}

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

int main(void){
  int n,x,y; cin >> n >> x >> y;
  int dis[n];
  rep(i,n) dis[i] = 0;
  for(int i = 1; i <= n-1; i++){
    for(int j = i + 1; j <= n; j++){
      dis[min(abs(j-i),abs(x-i)+1+abs(y-j))]++;
    }
  }
  for(int i = 1; i < n; i++){
    cout << dis[i] << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

// struct fast_io {
	// fast_io(){
		// std::cin.tie(nullptr);
		// std::ios::sync_with_stdio(false);
	// };
// } fio;


signed main(){
	cout<<fixed<<setprecision(10);
	
	int N, K;
	vector<int> P, C, X;
	int score = -INF;
	
	cin>>N>>K;
	
	P.resize(N);
	C.resize(N);
	X.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>P[i];
		P[i]--;
		
		X[P[i]] = i;
	}
	
	for(int i = 0; i < N; i++){
		cin>>C[i];
	}
	
	for(int i = 0; i < N; i++){
		// cout<<endl;
		// cout<<endl;
		// cout<<"i = "<<i<<" score = "<<score<<endl;
		// vector<int> used(N);
		vector<int> tsum;
		
		bool flag = false;
		int sum = 0, maximum = -INF;
		int now = i;
		int con = 0, rcon = 0;
		
		for(;;){
			
			
			now = P[now];
			
			sum += C[now];
			
			tsum.push_back(C[now]);
			
			con++;
			
			score = max(score, sum);
			maximum = max(maximum, sum);
			
			
			// cout<<"sum = "<<sum<<" con = "<<con<<" now = "<<now<<endl;
			
			// cout<<"K = "<<K<<" i = "<<i<<endl;
			if(con >= K) {
				flag = true;
				break;
			}
			
			if(now == i) break;
			// cout<<"END"<<endl;
		}
		
		if(flag) continue;
		
		if(K <= con) continue;
		
		// if(sum < 0) continue;

		rcon = con % K;
		
		
		rcon += con;
		sum = sum * (K / con - 1);
		
		// cout<<"sum = "<<sum<<" con = "<<con<<" "<<rcon<<endl;
		
		score = max(score, sum);
		
		for(int i = 0; i < rcon; i++){
			if(i + 1 + (K/con - 1) * con > K) break;
			
			sum += tsum[i%con];
			
			score = max(score, sum);
		}
	}
	
	cout<<score<<endl;
	
	return 0;
}

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

using namespace std;

using ll = long long;
using ld = long double;

const int INF = (1 << 30) - 1;
const ll INF64 = ((ll)1 << 62) - 1;
const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

	int n;
	cin >> n;

	vector<ll> l(n + 3);
	l[0] = 2;
	l[1] = 1;
	for (int i = 2; i <= n; i++) {
		l[i] = l[i - 1] + l[i - 2];
	}

	cout << l[n] << endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define rREP(i,m,n) for(int (i)=(n)-1; (i)>=(m);--(i))
#define all(x) (x).begin(),(x).end()
#define out(y,x,h,w) (y)<0||(x)<0||(y)>=(h)||(x)>=(w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

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;
}
inline void init() {
	cin.tie(nullptr); cout.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

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

int main() {
	init();

	int H, W; cin >> H >> W;
	vector<string>maze(H);
	rep(i, H)cin >> maze[i];

	Graph memo(H, vector<int>(W, -1));
	queue <pair<int, int>>XY;
	memo[0][0] = 1;
	XY.push(make_pair(0, 0));
	while (!XY.empty()) {
		pair<int, int>xy = XY.front();
		XY.pop();
		rep(i, 4) {
			int nx = xy.first + dx[i], ny = xy.second + dy[i];
			if (out(ny, nx, H, W) || maze[ny][nx] == '#' || memo[ny][nx] != -1)continue;
			memo[ny][nx] = memo[xy.second][xy.first] + 1;
			XY.push(make_pair(nx, ny));
		}
	}
	int cnt = memo[H - 1][W - 1];
	if (cnt == -1) {
		cout << cnt << "\n";
		return 0;
	}
	rep(i, H)rep(j, W)if (maze[i][j] == '#')++cnt;
	cout << H * W - cnt << "\n";
		
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h> // uint64_t

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

int get_int(void) {
  int num;
  scanf("%d", &num);
  return num;
}
#define ULIMIT 55555
#define NUM_MAX (ULIMIT*5)
#define SET_MAX 55

static int map[5][ULIMIT];
static int idx[5];

void push(int val) {
    int mod = val%5;
    map[mod][idx[mod]++] = val;
}

int main(void) {
    int num = get_int();
    static char prime[NUM_MAX];
    memset(prime, 0x01, NUM_MAX);
    // sieve
    int i, j;
    for(i = 2; i <= ULIMIT; i++) {
        int ulimit = ULIMIT/i;
        if(!prime[i]) continue;
        for(j = 2; j <= ulimit; j++) {
            prime[i*j] = 0;
        }
    }

    for(i = 2; i <= ULIMIT; i++) {
        if(!prime[i]) continue;
        push(i);
    }

    int argmax = 0;
    int cnt_max = 0;
    for(i = 0; i < 5; i++) {
        if(cnt_max < idx[i]) {
            argmax = i;
            cnt_max = idx[i];
        }
    }
#ifdef DEBUG
    printf("%d argmax: %d\n", cnt_max, argmax);
#endif
    for(i = 0; i < num; i++) {
        printf("%d\n", map[argmax][i]);
    }
    return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <math.h>
#include <sstream>
#include <iterator>

using namespace std;

#define endl ("\n")
#define pi (3.141592653589)
#define mod 1000000007
#define int int64_t
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

struct debugger
{
    template<typename T> debugger& operator , (const T& v)
    {    
        cerr<<v<<" ";    
        return *this;    
    }
} dbg;

int minDifferenceAmongMaxMin(int arr[], int N, int K) 
{ 
    sort(arr, arr + N); 
    int res = INT_MAX; 
    for (int i = 0; i <= (N - K); i++) 
    { 
        int curSeqDiff = arr[i + K - 1] - arr[i]; 
        res = min(res, curSeqDiff); 
    } 
  
    return res; 
} 

int32_t main() 
{
    fast;
	int N, K;
	cin >> N >> K;
	int x[N+1];
	rep(i, N){
       int n; 
	   cin >> n;
	   x[i] = n;
	}
	cout << minDifferenceAmongMaxMin(x, N, K);
}





Measured memory_bytes: 4384
//自称高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INF 1999999999
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007 

int main() {
    int ans = 0;
    int tmp;
    int a, b;
    cin >>a >> b;

    cout << (a+b)%24 << endl;
}

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

#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a	b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int table[365][26];
int main()
{	
		int D;
	cin >> D;
	vec(C);
	vec(Las);
	fx(26) {
		vecp(C);
		Las.push_back(0);
	}

	fx(D) {
		fy(26) {
			cin >> table[x][y];
		}
	}
	vector<int> T(D);
	int k = 0;
	fx(26) {
		if (k < table[0][x]) {
			T[0] = x+1;
			k = table[0][x];
		}
	}
	fx(D-1) {
		ll res = -LLONG_MAX;
		int las = 0;
		fy(26) {
			ll sum = 0;
			sum += table[x + 1][y];
			fz(26){
				if (y == z) { continue; }
				sum -= C[z] * (x + 1 - Las[z]);
			}

			if (res<sum) {
				res = sum;
				T[x + 1] = y+1;
				las = y;
			}
		}
		Las[las] = x + 1;
	}

	ll sum = -INT_MAX,sumPre=-INT_MAX;
	srand(time(NULL));
	fx(15000) {
		int d, q;
		d = rand() % D + 1;
		q = rand() % 26 + 1;
		if (sum==sumPre) {
			int d3 = rand() % 4;
			while (d3==0) {
				d3 = rand() % 4;
			}
			int sign = rand() % 2;
			int d2;
			if (sign==0) {
				d2 = min(d + d3,D);
			}
			else {
				d2 = max(d - d3,1);
			}
			T[d - 1] ^= T[d2 - 1];
			T[d2 - 1] ^= T[d - 1];
			T[d - 1] ^= T[d2 - 1];
		}
		int pre = T[d - 1];

		T[d - 1] = q;
		ll ans = 0;

		fy(26) {
			Las[y] = 0;
		}
		fw(D) {

			ans += table[w][T[w] - 1];
			Las[T[w] - 1] = w + 1;
			fz(26) {
				ans -= C[z] * (w + 1 - Las[z]);
			}

		}
		if (ans > sum) {
			sum = ans;
		}
		else {
			T[d - 1] = pre;
		}
	}


	fx(D) {
		ce(T[x])
	}
    return 0;
}



Measured memory_bytes: 3688
s = gets.chomp.split('')

res = ''
tmp = []
s.size.times do |i|
  if s[0]=='L' && s[1]=='R' || s[1].nil?
    tmp << s.shift
    index = tmp.find_index{|i| i =='L'}
    n = tmp.size
    r = l = n / 2
    l += 1 if n.odd? && index.even?
    r += 1 if n.odd? && index.odd?
    l_0 = ''
    r_0 = ''
    (index-1).times do
      l_0.concat('0 ')
    end
    (n-index-1).times do
      r_0.concat('0 ')
    end
    res.concat(l_0 + r.to_s + ' ' + l.to_s + ' ' + r_0)
    tmp = []
  else
    tmp << s.shift
    next
  end
end

print(res)

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

/*==========type============*/
typedef long long ll;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
typedef std::pair<std::string,ll> psl;

/*==========constant============*/
#define INF INT_MAX
#define INF_L LONG_MAX
#define INF_LL LLONG_MAX
#define MOD 1000000007

/*==========function============*/
#define REP(ii,jj,nn) for (ll ii=jj;ii<(nn);ii++)
#define RREP(ii,nn,jj) for (ll ii = nn; jj<ii;ii--)
#define each(i,...) for (auto&& i:__VA_ARGS__)
#define ALL(vec) (vec).begin(),(vec).end()
#define sum(...) accumulate(ALL(__VA_ARGS__),0LL)
#define dsum(...) accumulate(ALL(__VA_ARGS__),0.0)
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
template<class T> inline auto max(const T& a){ return *max_element(ALL(a)); }
template<class T> inline auto min(const T& a){ return *min_element(ALL(a)); }
inline ll gcd(ll a,ll b){if(b == 0) return a;return  gcd(b,a%b);}
inline ll lcm(ll a,ll b){ll g = gcd(a,b);return a / g * b;}

using namespace std;

ll n,m,x;
vector<ll> c(12);
vector<ll> a[12];

bool eval(vector<ll> aa){
    REP(i,0,aa.size()){
        if(aa[i] < x)return false;
    }
    return true;
}

ll dfs(ll index, ll cost,vector<ll>& aa){
    if(eval(aa))return cost;
    if(index >= n)return INF;

    ll ret = INF;
    
    vector<ll> aaa = aa;
    REP(i,0,aaa.size())aaa[i] += a[index][i];
    ret = min(ret,dfs(index+1,cost+c[index],aaa));

    ret = min(ret,dfs(index+1,cost,aa));

    return ret;
}

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

    cin >> n >> m >> x;
    REP(i,0,n){
        cin >> c[i];
        REP(j,0,m){
            ll aa;
            cin >> aa;
            a[i].push_back(aa);
        }
    }
    
    vector<ll> aaa(m,0);
    ll ans = dfs(0,0,aaa);
    if(ans == INF)ans = -1;
    
    cout << ans << endl;

    return 0;
}    

Measured memory_bytes: 3664
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];

ll f(ll l,ll r){
  if(dp[l][r] != -1)return dp[l][r];
  if(r-l <= 0)return dp[l][r] = 0;
  ll ret = 0;
  rep(i,l,r){
    ret = max(ret,f(l,i)+f(i+1,r));
  }
  if(abs(d[l]-d[r]) <= 1){
    if(f(l+1,r-1) == r-l-1){
      ret = max(ret,f(l+1,r-1)+2);
    }
  }
  return dp[l][r] = ret;
}

int main(){
  int n;
  while(1){
    cin >> n;
    if(n == 0)break;
    rep(i,0,n)cin >> d[i];
    clr(dp,-1);
    cout << f(0,n-1) << endl;
  }
  return 0;
}



Measured memory_bytes: 7116
while True:
    n,w,d = list(map(int,input().split()))
    if n == 0 and w == 0 and d == 0:
        exit()
    SP = [list(map(int,input().split())) for _ in range(n)]
    
    C = {}
    C[1] = {'height': d, 'width': w}
    
    for sp in SP:
        cake = C.pop(sp[0])
        p = sp[1]%(2*cake['width']+2*cake['height'])
        max_index = max(list(C.keys())+[0])
        if 0 < p < cake['width']:
            C[max_index+1] = {'height': cake['height'], 'width': min(p,cake['width']-p)}
            C[max_index+2] = {'height': cake['height'], 'width': max(p,cake['width']-p)}
        elif cake['width'] < p < cake['width']+cake['height']:
            C[max_index+1] = {'height': min(p-cake['width'],cake['width']+cake['height']-p), 'width': cake['width']}
            C[max_index+2] = {'height': max(p-cake['width'],cake['width']+cake['height']-p), 'width': cake['width']}
        elif cake['width']+cake['height'] < p < 2*cake['width']+cake['height']:
            C[max_index+1] = {'height': cake['height'], 'width': min(p-cake['width']-cake['height'],2*cake['width']+cake['height']-p)}
            C[max_index+2] = {'height': cake['height'], 'width': max(p-cake['width']-cake['height'],2*cake['width']+cake['height']-p)}
        else:
            C[max_index+1] = {'height': min(p-2*cake['width']-cake['height'],2*cake['width']+2*cake['height']-p), 'width': cake['width']}
            C[max_index+2] = {'height': max(p-2*cake['width']-cake['height'],2*cake['width']+2*cake['height']-p), 'width': cake['width']}
        
        new_C = {}
        for i,key in enumerate(sorted(C.keys())):
            new_C[i+1] = C[key]
        C = new_C
    
    ans = []
    for c in C.values():
        ans.append(c['height']*c['width'])
    print(*sorted(ans))


Measured memory_bytes: 5656
object Main {
  def main(args: Array[String]): Unit = {
    solve
  }

  def solve(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt
    val l = new Array[Int](n)
    for (i <- 0 until n) {
      l(i) = sc.nextInt
    }
    val sortedL = l.sorted

    var count = 0
    for (i <- 0 until n - 2; j <- i + 1 until n - 1) {
      val a = sortedL(i)
      val b = sortedL(j)

      var flg = true
      var k = n - 1
      while (flg && k >= j + 1) {
        val c = sortedL(k)
        if (c < a + b) {
          flg = false
        } else {
          k -= 1
        }
      }
      count += k - j
    }
    println(count)
  }
}


Measured memory_bytes: 28416
#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0); 
	int t=1,q;
	//cin>>t;
	for(q=1;q<=t;q++)
	{
		int n,m;
		cin>>n>>m;
		long long int a[n][m],i,j,k;
		for(i=1;i<n;i++)
		{
			for(j=1;j<m;j++)
			{
				a[i][j]=-1;
			}
		}
		a[0][0]=1;
		string s[n];
		for(i=0;i<n;i++)cin>>s[i];
		for(i=1;i<n;i++)
		{
			a[i][0]=a[i-1][0];
			if(s[i][0]=='#')a[i][0]=0;
		}
		for(i=1;i<m;i++)
		{
			a[0][i]=a[0][i-1];
			if(s[0][i]=='#')a[0][i]=0;
		}
		for(i=1;i<n;i++)
		{
			for(j=1;j<m;j++)
			{
				if(s[i][j]=='#')a[i][j]=0;
				else a[i][j]=(a[i-1][j]%1000000007+a[i][j-1]%1000000007)%1000000007;
			}
		}
		cout<<a[n-1][m-1]<<"\n";
	}
	return 0;
}

Measured memory_bytes: 9088
while 1:
    n=int(input())
    if n==0:break
    c=1
    nx=ny=int((n-1)/2)
    b=[[0 for _ in range(n)] for _ in range(n)]
    ny=ny+1
    b[ny][nx]=c

    for _ in range(n*n-1):
        c+=1
        nx+=1
        ny+=1
        while 1:
            if 0<=nx<=n-1 and 0<=ny<=n-1 and b[ny][nx]==0:
                break
            if nx>n-1:
                nx=0
            if nx<0:
                nx=n-1
            if ny>n-1:
                ny=0
            if b[ny][nx]==0:
                break
            else:
                nx-=1
                ny+=1
        b[ny][nx]=c
    for i in b:
        print(("".join([str(l).rjust(4) for l in i])))



Measured memory_bytes: 5612
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	ll ans=0;
	for(int a=1;a<=n;a++){
		if((a+a)%k==0){
			int minus=(a)/k;
			ans+=(ll)((n+a)/k-minus)*((n+a)/k-minus);
		}
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <limits.h>
#include <numeric>
#include <math.h>
#include <bits/stdc++.h>
#define rep(i,c) for (int i=0;i<c;i++)
using namespace std;
 
 
int main(){
    int N,M,C;
    cin >> N >> M >> C;
    int B[M];
    rep(i,M) cin >> B[i];
    int A[N][M];
    rep(i,N){
        rep(j,M)cin >> A[i][j];
    } 
    int ans =0;
    rep(i,N){
        int count = C;
        rep(j,M){
            
            count += A[i][j]*B[j]; 
        }
        if(count>0)ans++;
    }
    cout << ans <<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int n,k,sum,A;
int main()
{
    cin>>n >>k;
    for(int i=0;i<n;i++)
    {
        cin>>A;
       sum=sum+2*min(A,k-A);
    }
    printf("%d",sum);
    
   
    
  
   
    
}



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

using namespace std;

int main() {
   ios_base::sync_with_stdio(false); cin.tie(nullptr);
   int N, V;
   cin >> N >> V;
   vector<int> X(N);
   for (int i = 0; i < N; ++i) cin >> X[i];
   vector<int> vs = {V};
   while (vs.back()) vs.emplace_back(vs.back() / 2);
   reverse(vs.begin(), vs.end());
   int sz = int(vs.size()) - 1;
   vector<vector<int>> L(sz + 1, vector<int>(N));
   vector<vector<int>> R(sz + 1, vector<int>(N));
   for (int i = 0; i <= sz; ++i) {
      L[i][0] = 0;
      for (int j = 1; j < N; ++j) {
         if (X[j] - X[j - 1] <= vs[i]) L[i][j] = L[i][j - 1];
         else L[i][j] = j;
      }
      R[i][N - 1] = N - 1;
      for (int j = N - 2; j >= 0; --j) {
         if (X[j + 1] - X[j] <= vs[i]) R[i][j] = R[i][j + 1];
         else R[i][j] = j;
      }
   }
   vector<int> forw(1 << sz, -1);
   vector<int> backw(1 << sz, N);
   for (int mask = 0; mask < (1 << sz); ++mask) {
      for (int i = 0; i < sz; ++i) if (!(mask & (1 << i))) {
         int nmask = mask | (1 << i);
         forw[nmask] = max(forw[nmask], R[i][min(forw[mask] + 1, N - 1)]);
         backw[nmask] = min(backw[nmask], L[i][max(backw[mask] - 1, 0)]);
      }
   }
   vector<int> cnt(N);
   for (int mask = 0; mask < (1 << sz); ++mask) {
      int lv = forw[mask];
      int rv = backw[mask ^ ((1 << sz) - 1)];
      int low = L[sz][max(rv - 1, 0)];
      int high = R[sz][min(lv + 1, N - 1)];
      if (low <= high) {
         ++cnt[low];
         if (high + 1 < N) --cnt[high + 1];
      }
   }
   for (int i = 0; i < N; ++i) {
      if (i) cnt[i] += cnt[i - 1];
      cout << (cnt[i] ? "Possible" : "Impossible") << "\n";
   }
}


Measured memory_bytes: 35824