akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
char maps[100][100];
int main()
{
	int H,W;
	cin>>H>>W;
	for(int i=0; i<H; i++) 
	{
		for( int j=0; j<W; j++ )
		{
			cin>>maps[i][j];
		}
	}

	int dx[2] = {1,0};
	int dy[2] = {0,1};
	map<pii,int> steps;
	steps[pii(0,0)] = maps[0][0] == '.' ? 0 : 1;
	queue<pii> que;
	que.push(pii(0,0));
	while(!que.empty())
	{
		pii cor = que.front();
		que.pop();
		
		for( int i=0; i<2; i++ )
		{
			int x = cor.first + dx[i];
			int y = cor.second + dy[i];
			if( x<H && y <W )
			{
				int step = steps[pii(cor.first,cor.second)];
				if(maps[x][y]=='#' && maps[x][y]!=maps[cor.first][cor.second])
					step++;
				if( steps.count(pii(x,y))==0 || step<steps[pii(x,y)] )
				{
					que.push(pii(x,y));
					steps[pii(x,y)] = step;
				}
				//cout << "x="<<x<<",y="<<y<<",map="<<maps[x][y]<<",step="<<step<<endl;
			}
		}	
	}
	cout << steps[pii(H-1,W-1)] <<endl;
	
	return 0;
}
/*
char a[1000000];
int main()
{
	int N;
	cin>>N;
	for(int i=0; i<N; i++)
		cin>>a[i];
	
	cout << a << endl;
}
*/


Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    cin >> str;
    int takahashi = 0;
    for(int i=0;i<4;i++){
        if(str.at(i) == '+'){
            takahashi++;
        }
        else takahashi--;
    }
    cout << takahashi;
    return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            List<string> ls = new List<string>();
            int kq = 0;
            while (n-- > 0)
            {
                var v = Console.ReadLine().ToCharArray();
                Array.Sort(v);
                string s = new string(v);
                ls.Add(s);
            }
            var a = ls.GroupBy(x => x).Select(x => (long)x.Count()*(x.Count()-1)/2).ToList() ;
            Console.WriteLine(a.Sum());
        }
    }
}

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

int main(){
    int N,M;
    string S;
    string T;
    map<string,int> map;
    cin >> N;    
    for(int i=0;i<N;i++){
        cin>>S;
        map[S]++;

    }

    cin >> M;
    for(int j=0;j<M;j++){
        cin>>T;
        map[T]--;
    }

    int ans = 0;
    for(auto x:map){

        ans=max(ans,x.second);
    }

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

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a; i< (int)(b); ++i)
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
typedef long double ld;
using ll=long long;
using namespace std;
const int mod=1e9+7;
const ll inf = 1LL << 60;
const ld pi = (acos(-1));
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }




int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  
  string s; cin >> s;
  int ans = 10000;
  rep(i,0,s.size()-2){
    int a = (s[i]-'0')*100 + (s[i+1]-'0')*10 + (s[i+2]-'0');
    chmin(ans,abs(753-a));
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
function Main(input) {
  input = input.split("\n");
  var N = parseInt(input[0].split(" ")[0]);
  var K = parseInt(input[0].split(" ")[1]);
  var S = input[1].split("");
  S[K-1]=S[K-1].toLowerCase();
  var ans=S.join("");
  console.log('%s', ans);
  return ans;
}

function debug(n){
  var input = document.getElementById("input"+n).value;
  var ans=Main(input);
  var result="WA";
  if(ans==document.getElementById("output"+n).value.split("\n")[0]){
    result="AC";
  }
  document.getElementById("result"+n).innerHTML=result;
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 21064
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000050;
typedef long long ll;
const ll MOD = 998244353;
int a[maxn], cnt[maxn], inv[maxn], mu[maxn];
void init()
{
    inv[1] = 1;
    for (int i = 2; i < maxn; ++i)
    {
        inv[i] = (ll)inv[MOD % i] * (MOD - MOD / i) % MOD;
    }
    mu[1] = 1;
    for (int i = 1; i < maxn; ++i)
    {
        for (int j = i+i; j < maxn; j += i)
            mu[j] -= mu[i];
    }
}
ll f[maxn];
ll solve1(int n, int m)
{
    ll ans = 0;
    for (int d = 1; d <= m; ++d)
    {
        ll t = 0;
        for (int i = 1; i <= m / d; ++i)
        {
            for (int j = 1; j <= m / d; ++j)
            {
                if (__gcd(i, j) != 1)
                    continue;
                t = (t + (ll)i * j % MOD * cnt[i * d] % MOD * cnt[j * d] % MOD) % MOD;
            }
        }
        ans = (ans + t * d) % MOD;
    }
    for (int i = 1; i <= m; ++i)
    {
        if (!cnt[i])
            continue;
        ans = (ans - (ll)cnt[i] * i) % MOD;
    }
    ans = ans * inv[2] % MOD;
    if (ans < 0)
        ans += MOD;
    return ans;
}
int main()
{
    // string s;
    // while(cin>>s) cout<<s<<endl;
    init();
    int n;
    scanf("%d", &n);
    int m = 1;
    for (int i = 1; i <= n; ++i)
        scanf("%d", a + i), cnt[a[i]]++, m = max(m, a[i]);

    for (int i = 1; i <= m; ++i)
    {
        for (int j = i; j <= m; j += i)
        {
            f[i] = (f[i] + (ll)j / i * cnt[j]) % MOD;
        }
    }
    ll ans = 0;
    for (int d = 1; d <= m; ++d)
    {
        ll t = 0;
        for (int k = 1; k <= m / d; ++k)
        {
            t = (t + (ll)mu[k] * k * k % MOD * f[d * k] % MOD * f[d * k] % MOD) % MOD;
        }
        ans = (ans + t * d) % MOD;
    }
    for (int i = 1; i <= m; ++i)
    {
        if (!cnt[i])
            continue;
        ans = (ans - (ll)cnt[i] * i) % MOD;
    }
    ans = ans * inv[2] % MOD;
    if (ans < 0)
        ans += MOD;
    printf("%lld\n", ans);
    system("pause");
    return 0;
}

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

#define MOD 1000000007

long long fib_memo[100005] = {0};

long long fib(long n) {
    if (n == 0) {
        return 1;
    }
    if (n == 1) {
        return 1;
    }
    if (fib_memo[n] != 0) {
        return fib_memo[n];
    }
    return fib_memo[n] = (fib(n - 1) + fib(n - 2)) % MOD;
}

int main() {
    long n, m;
    cin >> n >> m;

    if (m == 0) {
        cout << fib(n) << endl;
        return 0;
    }

    long A[100005];
    for (long i = 0; i < m; i++) {
        cin >> A[i];
    }

    // break 2 steps => ans = 0
    for (long i = 1; i < m; i++) {
        if (A[i] == A[i - 1] + 1) {
            cout << 0 << endl;
            return 0;
        }
    }

    long long ans;
    for (long i = 0; i <= m; i++) {
        if (i == 0) {
            ans = fib(A[0] - 1) % MOD;
        }
        else if (i == m) {
            ans *= fib(n - A[m - 1] - 1) % MOD;
        }
        else {
            ans *= fib(A[i] - A[i - 1] - 2) % MOD;
        }
        ans = ans % MOD;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 4096
n, a, b, c, d = gets.chomp.split(' ').map(&:to_i)
s = gets.chomp

if c > d && c != n
	s.slice!(c..(n-1))
elsif d > c && d != n
	s.slice!(d..(n-1))
end

if a > 1
	s.slice!(0..(a-1-1))
end

if s.include?('##')
	puts 'No'
	exit
end

if a < c && c < b
	puts 'Yes'
	exit
end

if c < d
	puts 'Yes'
	exit
end

s.slice!((d+2-1)..(s.length-1))
if b - a > 1 
	s.slice!(0..(b-a-1-1))
end

if s.include?('...')
	puts 'Yes'
else
	puts 'No'
end


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

#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

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

  vector<pair<ll, ll>> p(n);
  for (auto& x : p) {
    cin >> x.first >> x.second;
  }

  sort(p.begin(), p.end());

  double sum = 0;
  ll count = 0;
  do {
    double tmp = 0;
    for (long i = 0; i < n - 1; i++) {
      ll dx = p.at(i).first - p.at(i + 1).first;
      ll dy = p.at(i).second - p.at(i + 1).second;
      tmp += sqrt(dx * dx + dy * dy);
    }
    sum += tmp;
    count++;

  } while (next_permutation(p.begin(), p.end()));
  cout << std::fixed << std::setprecision(64) << sum / count << endl;
}


Measured memory_bytes: 3808
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

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

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

void solve(long long N, std::vector<long long> H) {
  int cnt = 0;
  int ans =0;
  bool flag = true;
  H.PB(10000000000);
  REP(i, N){
    // cout<<H[i]<<","<<cnt<<endl;
    if(H[i]>=H[i+1]){
      cnt++;
    }
    else{
      ans = max(ans, cnt);
      cnt=0;
    }
  }
  cout<<ans<<endl;
}

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


Measured memory_bytes: 1792
from typing import List, Any


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def solve() -> Any:
    S = read_int()
    dp = [[0 for _ in range(S+1)] for _ in range(S//3+1)]
    prefix = [1]*(S+1)
    new_prefix = [0]*(S+1)
    modulo = 10**9+7
    for i in range(1, S//3+1):
        for j in range(1, S+1):
            if i > 0 and j >= 3:
                dp[i][j] = (dp[i][j]+prefix[j-3])%modulo
            new_prefix[j] = (new_prefix[j-1]+dp[i][j])%modulo
        prefix, new_prefix = new_prefix, prefix
        new_prefix[0] = 0
    #dp[i][j] = dp[i-1][j-k] # i number of elements such that sum is j, 3 <= k <= 9
    return sum(dp[i][S] for i in range(1, S//3+1))%modulo


if __name__ == '__main__':
    print(solve())


Measured memory_bytes: 81024
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
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;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const ll mod=1e9+7;
const int inf=1<<30;

int main(){
  int n; cin >> n;
  vector<ll> a(n);
  rep(i,n) cin >> a[i];
  vector<ll> b(n);
  rep(i,n) cin >> b[i];
  vector<ll> f(n,0);
  f[0]=a[0]; f[n-1]=b[n-1];
  ll ans=1;
  for(int i=1;i<n-1;i++){
    if(a[i-1]==a[i]&&b[i]==b[i+1]){
      ans*=min(a[i],b[i]);
      ans%=mod;
    }
    else if(a[i-1]<a[i]&&b[i]>b[i+1]&&a[i]!=b[i]){
      cout << 0 << endl;
      return 0;
    }
    else if(a[i-1]<a[i]){
      f[i]=a[i];
    }
    else if(b[i]>b[i+1]){
      f[i]=b[i];
    }
  }
  vector<ll> aa=f;
  vector<ll> bb=f;
  rep(i,n-1){
    chmax(aa[i+1],aa[i]);
  }
  for(int i=n-2;i>=0;i--){
    chmax(bb[i],bb[i+1]);
  }
  if(a==aa&&b==bb){
    cout << ans << endl;
  }
  else cout << 0 << endl;
}

Measured memory_bytes: 4224
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000009;

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

signed main(){
	int m,n;
	double p[20][20] = {};
	cin >> m >> n;
	for(int i = 0;i < m;i++){
		for(int j = 0;j < n;j++) cin >> p[i][j];
	}
	double dp[1 << 16][20] = {};
	for(int i = 0;i < (1 << m);i++) dp[i][n] = 1;
	for(int i = (1 << m) - 2;i >= 0;i--){
		for(int j = n - 1;j >= 0;j--){
			for(int k = 0;k < m;k++){
				if((i >> k) & 1) continue;
				double sum = 0,q = 1;
				for(int l = j;l <= n;l++){
					sum += dp[i | (1 << k)][l] * q * (1.0 - p[k][l]);
					q *= p[k][l];
				}
				chmax(dp[i][j],sum);
			}
		}
	}
	printf("%.15lf\n",dp[0][0]);
}


Measured memory_bytes: 13536
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    string s;
    unordered_map<string,int> m;
    for(int i=0;i<n;i++){cin>>s;m[s]++;}
    cout<<"AC x "<<m["AC"]<<"\n";
    cout<<"WA x "<<m["WA"]<<"\n";
    cout<<"TLE x "<<m["TLE"]<<"\n";
    cout<<"RE x "<<m["RE"]<<"\n";
    
}

Measured memory_bytes: 3628
#include <cstdio>
int main(){int n,i,j,s,m,a[5000];while(scanf("%d", &n)&&n){for(i=0;i<n;i++)scanf("%d",&a[i]);m=-2147483647;for(i=0;i<n;i++,s=0)for(j=i;j<n;j++)if((s+=a[j])>m)m=s;printf("%d\n",m);}}

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

template<typename T>
T get_value(std::istream& is);

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

  vector<int> v(get_value<size_t>(cin));
  for (auto& e: v)
    cin >> e;
  sort(v.begin(), v.end(), greater<int>{});

  int a {0}, b {0};
  for (auto i {0u}; i < v.size(); ++i) {
    if (i % 2)
      b += v[i];
    else
      a += v[i];
  }

  cout << a - b << '\n';
  return 0;
}

template<typename T>
inline T get_value(std::istream& is)
{
  T v;
  is >> v;
  return v;
}


Measured memory_bytes: 256
import java.util.*;

class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int[] ret = new int[a];
        int wk;
        for(int i =0;i<a;i++){
            wk =Integer.parseInt(sc.next());
            ret[wk-1] = i;
        }
        StringBuilder retstr = new StringBuilder();
        for(int x : ret){
            retstr.append(x+1).append(" ");
        }
        retstr.setLength(retstr.length()-1);
        System.out.println(retstr.toString());
        sc.close();
    }
}

Measured memory_bytes: 48392
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1] # s = s_in()
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()

N, M = ds_in()
S = s_in()
T = s_in()

# 最大公約数
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a


# 最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)

lcm_NM = lcm(N, M)
for i in range(N * M // lcm_NM):
    if S[i*lcm_NM//M] != T[i*lcm_NM//N]:
        print(-1)
        exit()

print(lcm_NM)


Measured memory_bytes: 3316
#include<iostream>


struct Watch{
  int h, m, s;
};


Watch time(int S) {
  Watch hms;
  hms.h = S / 3600;
  S -= hms.h * 3600;
  hms.m = S / 60;
  hms.s = S - hms.m * 60;

  return hms;
}


int main() {
  int S;
  char delimiter = ':';
  std::cin >> S;

  Watch hms = time(S);

  std::cout << hms.h << delimiter << hms.m << delimiter << hms.s << std::endl;

  return 0;
}



Measured memory_bytes: 3036
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	ll n,m;
	cin	>> n>> m;
	ll ans = 0;
	if (n == 1 && m == 1){
		ans = 1;
	}
	else if(n == 1 || m == 1){
		ans = max(n,m) - 2;
	}
	else{
		ans = (m- 2) * (n - 2);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
$N,$A,$B,$C = gets.split.map(&:to_i)
$L = Array.new($N) {gets.to_i}
$INF = 10**9
$min = $INF

class Node
  attr_accessor :depth, :mp, :a, :b, :c

  def initialize(depth, mp, a, b, c)
    @depth = depth
    @mp = mp
    @a = a
    @b = b
    @c = c
  end
end

def calc(v)
  if [v.a,v.b,v.c].min == 0
    return $INF
  else
    return v.mp + (v.a-$A).abs+(v.b-$B).abs+(v.c-$C).abs
  end
end

def dfs()
  stack = [Node.new(0,-30,0,0,0)]

  while !stack.empty? do

    v = stack.pop()

    if v.depth > $N-1 then
      c = calc(v)
      $min = c if $min > c
    else
      stack.push(Node.new(v.depth+1,v.mp+10,v.a+$L[v.depth-1], v.b, v.c))
      stack.push(Node.new(v.depth+1,v.mp+10,v.a, v.b+$L[v.depth-1], v.c))
      stack.push(Node.new(v.depth+1,v.mp+10,v.a, v.b, v.c+$L[v.depth-1]))
      stack.push(Node.new(v.depth+1,v.mp,v.a, v.b, v.c))
    end
  end
end

dfs()
puts $min


Measured memory_bytes: 3964
#include <stdio.h>
int main(void){
    int a, b, c, ans = 0, i;
    
    scanf("%d %d %d", &a, &b, &c);
    
    for(i = a; i <= b; i++){
        if(c % i == 0) ans++;
    }
    printf("%d\n", ans);
    
    return 0;
}


Measured memory_bytes: 2128
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i,s,n) for(int i = (s); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for(int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n';
#define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;

int main(void)
{
    IOS
    const int A = 26;
    int D; cin >> D;
    vector<int> c(A);
    REP(i,A) cin >> c[i];
    vector<vector<int>> s(D,vector<int>(A));
    REP(i,D) REP(j,A) cin >> s[i][j];

    vector<int> a(D);
    REP(i,D) {
        cin >> a[i];
        a[i]--;
    }
    int m; cin >> m;

    
    REP(_,m) {
        vector<int> last(A,-1);
        int score = 0;
        int d, q;
        cin >> d >> q;
        d--;q--;
        a[d] = q;
        REP(i,D) {
            score += s[i][a[i]];
            last[a[i]] = i;
            REP(j,A) {
                score -= c[j]* (i-last[j]);
            }
        }
        OUT(score)
    }
    
    return 0;
}

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

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}

void code() {
    ll n;
    cin>>n;
    ll ans=0;
    vector<ll> A(n);
    for(ll i=0; i<n; i++) {
        cin>>A[i];
    }
    A.push_back(0);
    ll rem=1000;
    ll stocks=0;
    ll i=0;
    vector<bool> peaks(n);
    vector<bool> valleys(n);
    for(ll i=1; i<n; i++) {
        if(A[i]>=A[i-1] && A[i]>A[i+1])
            peaks[i]=true;
        if(A[i]<=A[i-1] && A[i]<A[i+1])
            valleys[i]=true;
    }
    if(A[0]<A[1])
        valleys[0]=true;
    for(ll i=0; i<n; i++) {
        if(valleys[i]) {
            stocks+=rem/A[i];
            rem%=A[i];
        }
        else if(peaks[i]) {
            rem+=(stocks*A[i]);
            stocks=0;
        }
    }

//    while(i<n) {
//        while(i<n && A[i]==A[i+1])
//            i++;
//        if(i<n && A[i]<A[i+1]) {
//            stocks = rem/A[i];
//            rem%=A[i];
//        }
//        while(i<n && A[i]<=A[i+1])
//            i++;
//        rem+=(stocks*A[i-1]);
//        stocks=0;
//        while(i<n && A[i]>=A[i+1]) {
//            i++;
//        }
//        stocks=rem/A[i-1];
//        rem%=A[i-1];
//    }


    cout << rem + stocks*A.back() << "\n";
}

signed main() {
    std::ios_base::sync_with_stdio(false);
    ll t=1;
    // cin>>t;
    for(ll i=0; i<t; i++) {
        code();
    }
}


Measured memory_bytes: 3640
import java.util.*;
import java.lang.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		String S = sc.next();
		String T = sc.next();
		int ss = S.length();
		int tt = T.length();
		char[] chS = new char[ss];
		char[] chT = new char[tt];
		for( int i=0; i<ss; i++ ){
			chS[i] = S.charAt(i);        
		}
		for( int i=0; i<tt; i++ ){
			chT[i] = T.charAt(i);       
		}
		int x = 0,y = 0,b = 0;
		loop:for( int i=0; i<=ss-tt; i++ ){
			int a = i;
			int count = 0;          
			for( int j=0; j<tt; j++ ){
				if( chS[a]=='?' || chS[a]==chT[j] ){
					count++;
				}
				if( count==tt ){
					x = i;
					y = i+tt-1;	
					b = 1;                  
				}  
				a++;              
			}            
		}
		if( ss==1 ){
			b = 0;          
        }          
		if( b==1 ){   //if start   
		for( int i=0; i<x; i++ ){
			if( chS[i]=='?' ){
				System.out.print("a");
			}else{
				System.out.print(chS[i]);
			}
		}
		for( int i=0; i<tt; i++ ){
			System.out.print(chT[i]);
		}
		for( int i=y+1; i<ss; i++ ){
			if( chS[i]=='?' ){
				System.out.print("a");
			}else{
				System.out.print(chS[i]);
			}
		}
        }      //if end
		if( ss==1 ){
			if( S.equals(T) || S.equals("?") ){
				System.out.println(T);              
            }else{
				System.out.println("UNRESTORABLE");              
            }              
        }else{
			if( x==0 && y==0 ){
				System.out.println("UNRESTORABLE");              
            }              
        }          
	}
}


Measured memory_bytes: 21844
/**
 *   created: 21 July 2019  12:00:05
**/
#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int , int> pii;
typedef vector< pair<int , int> > vii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long , long long> pll;
typedef vector< pair<long long , long long> > vll;

#define PB push_back
#define PPB pop_back
#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define rrep(i,a,b) for(int i = (int)(a-1); i>=(int)(b); i--)
#define INFLL 2000000000000000007
#define INF 2000000007
#define MOD 1000000007


#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif


void optimizeIO()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}

//                                        //AUTHOR: Kaushalesh Shukla

//=========================================================================================================
string s;
vector< vl > mem;
int n; 
void solve(int ind){
	if(ind>=n)
		return;
	if(s[ind]!='?'){
		if(ind>0){
			rep(i,0,13){
				int temp = i*10+s[ind]-'0';
				temp %= 13;
				mem[ind][temp] = (mem[ind][temp]%MOD + mem[ind-1][i]%MOD)%MOD;

			}
		}
		else{
			mem[ind][s[ind]-'0'] = 1;
		}
		solve(ind+1);
	}
	else{
		if(ind>0){
			rep(i,0,13){
				rep(j,0,10){
					int temp = i*10+j;
					temp %= 13;
					mem[ind][temp] = (mem[ind][temp]%MOD + mem[ind-1][i]%MOD)%MOD;
					// if(ind==n-1 && temp==5)
						// trace2(i,j);
				}
			}
		}
		else{
			rep(i,0,10){
				mem[ind][i] = 1;
			}
		}
		solve(ind+1);
	}
}
int main()
{
	optimizeIO();
	int tt=1;
	// cin>>tt;
	while(tt--){
		// string s;
		cin>>s;
		n = s.size();
		// rep(i,0,1000)
		// 	if(i%13==5)
		// 		cout<<i<<endl;
		mem.resize(n, vl(13,ll(0)));
		solve(0);
		// rep(i,0,n){
		// 	rep(j,0,13)
		// 		cout<<mem[i][j]<<" ";
		// 	cout<<endl;
		// }
		cout<<mem[n-1][5]<<endl;
	}
	return 0;
}


Measured memory_bytes: 13648
#include<iostream>

using namespace std;

int main(void) {
    int k, a, b; cin >> k >> a >> b;
    bool t = false;
    for (int i = k; i <= b; i += k) {
        if (a <= i && i <= b) t = true;
    }
    t ? cout << "OK" << endl : cout << "NG" << endl;
}

Measured memory_bytes: 3620
# Problem  https://atcoder.jp/contests/abc086/tasks/abc086_a
# ruby 2nd try

thisiseven = "Even"
thisisodd = "Odd"

first,second = gets.chomp("\n").split(" ").map(&:to_i)

# result = first*second
result = (first*second)%2
# puts(result)
if result == 0 then
    puts(thisiseven)
else
    puts(thisisodd) 
end


Measured memory_bytes: 3836
#include<iostream>
#include<string>
#include<stack>
#include<map>
#include<utility>
#include<vector>
#include<cmath>
#include <algorithm>

using namespace std;

int main(){

	string S,temp;
	typedef pair<string, int> P;
	vector <P>  arry (4);

	arry[0].first="dreamer"; arry[0].second=arry[0].first.size();
	arry[1].first="dream"; arry[1].second=arry[1].first.size();
	arry[2].first="eraser"; arry[2].second=arry[2].first.size();
	arry[3].first="erase"; arry[3].second=arry[3].first.size();

	//stack<string> temp;

	
	cin >> S;

	int num=0,ss;
	bool flag =0;
	bool can =1;

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

	for(int i=0; i<4; i++) reverse(arry[i].first.begin(),arry[i].first.end());

	for(int i=0; i<S.size();){

		bool can1=0;

		for(int j=0; j<4;j++){

			string d=arry[j].first;

			if( S.substr(i,d.size()) == d){


				can1=1;
				i+=d.size();
			}
		}

		if(can1==0){
			
			can =0;
			break;


		}
	}


	if(can)cout <<"YES" <<endl;

	else cout << "NO" <<endl;

}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
typedef long long lol;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(lol i=0;i<lol(n);i++)
#define REPD(i,n) for(lol i=n-1;i>=0;i--)
#define FOR(i,a,b) for(lol i=a;i<=lol(b);i++)
#define FORD(i,a,b) for(lol i=a;i>=lol(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい
#define SIZE(x) lol(x.size()) //sizeをsize_tからllに直しておく
//定数
#define PI 3.1415926535897932385 //pi
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//最大値最小値
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}

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

    if(n[0] == n[1] && n[1] == n[2])
        cout << "No" << endl;
    else
    {
        cout << "Yes" << endl;
    }

    return 0;
}

Measured memory_bytes: 3632
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1<<30
#define LINF 1LL<<60

/*
<url:https://www.ioi-jp.org/joi/2007/2008-ho-prob_and_sol/2008-ho.pdf>
?????????============================================================
??????????????¢??????WxH?????????????????????
?????????????????°???????????¢????????????????????????????????????
WxH?????????????????¢???????????????????????????????????????
=================================================================
?§£??¬=============================================================
================================================================
*/

/* Coordinate_Compression : ??§?¨???§???*/
class CC {
public:
	ll N;
	vector<ll> xs; // ????????¨:xs[x1 or x2????????????] := ????????§?¨?

	CC(ll N) :N(N) {}
	// x1, x2?????§?¨???§??????, ??§?¨???§???????????????????????????
	int compress(vector<ll>& x1, vector<ll>& x2, ll MIN_W = -1e10, ll MAX_W = 1e10) {
		for (int i = 0; i < N;i++) {
			ll tx1 = x1[i], tx2 = x2[i];
			if (MIN_W <= tx1 && tx1 <= MAX_W) xs.push_back(tx1);
			if (MIN_W <= tx2 && tx2 <= MAX_W) xs.push_back(tx2);
		}

		sort(xs.begin(), xs.end());
		xs.erase(unique(xs.begin(), xs.end()), xs.end());

		for (int i = 0; i < N;i++) {
			x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
			x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
		}

		return (int)xs.size();
	}
};

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

void dfs(int ny, int nx, vector<vector<int>>& masu, int range_h, int range_w) {
	masu[ny][nx] = 1;
	for (int i = 0; i < 4;i++) {
		int next_y = ny + dy[i];
		int next_w = nx + dx[i];
		if (next_y < 0 || next_y >= range_h)continue;
		if (next_w < 0 || next_w >= range_w)continue;
		if(masu[next_y][next_w] == 0) dfs(next_y, next_w, masu, range_h, range_w);
	}
}

int main(void) {
	cin.tie(0); ios::sync_with_stdio(false);
	ll w, h;
	while (cin >> w >> h, w | h) {
		ll N; cin >> N;
		vector<ll> x1(N + 1), y1(N + 1), x2(N + 1), y2(N + 1);
		x1[N] = 0, y1[N] = 0, x2[N] = w, y2[N] = h;
		for (int i = 0; i < N;i++) cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];

		CC CCx(N + 1), CCy(N + 1);
		ll W = CCx.compress(x1, x2, 0, w);
		ll H = CCy.compress(y1, y2, 0, h);

		vector<vector<int>> masu(H - 1, vector<int>(W - 1, 0));

		for (int i = 0; i < N;i++) {
			for (int j = x1[i]; j < x2[i];j++) {
				for (int k = y1[i]; k < y2[i];k++) {
					masu[k][j] = 1;
				}
			}
		}

		ll res = 0;
		for (int i = 0; i < H - 1;i++) {
			for (int j = 0; j < W - 1;j++) {
				if (masu[i][j] == 0) {
					res++;
					dfs(i, j, masu, H - 1, W - 1);
				}
			}
		}
		cout << res << endl;
	}
	return 0;
}

Measured memory_bytes: 8324
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
	string w;
	cin>>w;
	int cnt[26];
	for(int i=0;i<26;i++)cnt[i]=0;
	for(int i=0;i<w.size();i++){
		cnt[w[i]-'a']++;
	}
	for(int i=0;i<26;i++){
		if(cnt[i]%2==1){
			puts("No");
			return 0;
		}
	}
	puts("Yes");
}

Measured memory_bytes: 256
/*
	Look, if you had, one shot, or one opportunity
		To seize everything you ever wanted. In one moment
			Would you capture it,or just let it slip ?
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define nl cout<<endl

#define pii pair<ll,ll>
#define vi  vector<ll>
#define vii vector<pii>
#define mi  map<ll,ll>
#define all(a)  (a).begin(),(a).end()

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

#define test4(x,y,z,a) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<"		a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<"		y is "<<y<<"		z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<"		y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
#define N 300009

ll power(ll a,ll b,ll m)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}
int main()
{	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
        ll n;cin>>n;
	ll a[n+2];
	rep(i,1,n)
	cin>>a[i];
	ll ans=a[1]+a[n-1];
	rep(i,1,n-1)
	ans+=min(a[i],a[i+1]);
	cout<<ans<<endl;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define tmin(a,b,c) min((a),min((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> Vi;
typedef vector<vector<int> > VVi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int m[101][101]; //[w][h]

int main()
{
	while(1) {
		int n, s, t, w, h;
		// w = entire field width
		// h = entire field height
		// s = estate width
		// t = estate height
		memset(m, 0, sizeof(m));
		cin >> n;
		if (!n) break;
		cin >> w >> h;
		rep(i,0,n) {
			int a, b; cin >> a >> b;
			a--; b--;
			m[a][b]++;
		}
		cin >> s >> t;
		int ans = 0;
		rep(i,0,w-s+1) rep(j,0,h-t+1) {
			int cnt = 0;
			//printf("sw=%d sy=%d\n",i,j);
			rep(nw,i,i+s) rep(nh,j,j+t) {
				if (m[nw][nh]) cnt++;
			}
			ans = max(ans,cnt);
		}
		cout << ans << endl;
	}

	return 0;
}

Measured memory_bytes: 1208
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  
  int N;
  cin >> N;
  vector<int> button(N);
  int next = 1, count = 0;
  bool flag = false;
  
  for (int i = 0; i < N; i++) {
    cin >> button.at(i);
  }

  //for (int j = 0; j < N; j++) {
    //if (flag == false) {
      while (true) {
          int i = next;
          next = button.at(next - 1);
          button.at(i - 1) = 0;
          count++;
        //}
        if(next == 2) {
          cout << count << endl;
          //flag = true;
          break;
        }
   	    else if (next == 0) {
          cout << "-1" << endl;
          //flag = true;
      	  break;
        }
  	  }
    //}
  //}
}

Measured memory_bytes: 640
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <regex>
#define REP(i,n) for(int i = 0; i < n; i++)
#define FOR(i,j,n) for(int i = j; i < n; i++)
#define RFOR(i,j,n) for(int i = j-1; i >= n; i--)
#define PREC(n) fixed << setprecision(n)
#define print_array(v) REP(__k, v.size()) { cout << v[__k]; if(__k != v.size()-1) cout << " "; else cout << endl; }
#define YesorNo(a) printf(a?"Yes\n":"No\n")
#define _LIBCPP_DEBUG 0
using namespace std;
int64_t MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  cout << n * n << endl;
  return 0;
}
//templete by private_yusuke

Measured memory_bytes: 256
import sys
from collections import deque
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

H, W = rl()
grid = [list(rs()) for _ in range(H)]
INF = 1000

def bfs(sx, sy):
    d = [[INF] * W for _ in range(H)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque()
    que.append((sx, sy))
    d[sy][sx] = 0
    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]                
            if 0 <= ny and ny <= H-1 and 0 <= nx and nx <= W-1 and \
                    grid[ny][nx] != '#' and d[ny][nx] == INF:
                d[ny][nx] = d[p[1]][p[0]] + 1
                que.append((nx, ny))
    dis = [[0 if x == INF else x for x in row] for row in d]
    return max([max(y) for y in dis])

answer = 0

for s in range(H):
    for t in range(W):
        if grid[s][t] == '#':
            continue
        distance = bfs(t, s)
        if distance > answer:
            answer = distance

print(answer)


Measured memory_bytes: 3316
#include <iostream>
#include<cstdio>
#include<queue>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e7+5;

int main()
{int a;
cin>>a;
cout<<(3*a*a)<<endl;

    return 0;
}


Measured memory_bytes: 256
import numpy as np
n, k = map(int, input().split())
a = np.ones(n)
win = 0

for i in range(1,n+1):
  t = i
  while i < k:
    i = 2*i
    a[t-1] = (1/2)*a[t-1]
print((1/n)*sum(j for j in a))

Measured memory_bytes: 13252
#include <bits/stdc++.h>
#include <cstdlib> // abs(x-y)
#include <iomanip> // 出力の桁数指定
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;

int main() {
  int h, n;
  cin >> h >> n;
  
  int sum = 0;
  vector<int> a(n);
  rep (i, 0, n) {
    cin >> a.at(i);
    sum += a.at(i);
  }
  
  cout << (sum >= h ?"Yes" : "No" ) << endl;
  
}


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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
const long long INF = 1LL << 60;

int main() {
    int n;
    cin >> n;
    int count = 1;
    while(1) {
        if (n <= count * 1000) break;
        count++;
    }
    cout << count* 1000 - n << endl;

    return 0;
}

Measured memory_bytes: 3632
// 2018, Sayutin Dmitry.

#include <bits/stdc++.h>

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

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;

using std::pair;
using std::make_pair;

using std::tuple;
using std::make_tuple;
using std::get;

using std::min;
using std::abs;
using std::max;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()

#define pb push_back
#define eb emplace_back

int64_t log4(int64_t val) {
    int64_t res = 0;
    while (val > (int64_t(1) << (2 * res)))
        res += 1;

    return res;
}

int64_t log4xl(__int128_t val) {
    int64_t res = 0;
    while (val > (__int128_t(1) << __int128_t(2 * res)))
        res += 1;

    return res;
}


vector<vector<int64_t>> calc(vector<int> a) {
    const int n = SZ(a);
    vector<vector<int64_t>> f(n + 1, vector<int64_t>(32)); // [0; 31].

    for (int from = n - 2; from >= 0; --from) {
        int pos = from + 1;

        __int128_t num = a[pos - 1];
        for (int z = 0; z <= 31; ++z, num *= 4) {
            int64_t dv = log4xl((num + a[pos] - 1) / a[pos]);
            if (dv <= 30)
                f[from][z] = dv + f[from + 1][dv];
            else
                f[from][z] = dv + f[from + 1][30] + (dv - 30) * (n - pos - 1);
        }
    }

    // for (int from = n - 1; from >= 0; --from) {
    //     pre_a[from] = pre_a[from + 1];
        
    //     int pos = from + 1;
    //     if (pos < n) {
    //         int64_t dv = (a[pos - 1] + a[pos] - 1) / a[pos];
    //         pre_a[from] += log4(dv) * (n - pos);
    //     }
    // }
    return f;
}


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

    // code here
    int n = input<int>();
    vector<int> a(n);
    input_seq(ALL(a));

    vector<int> arev = a;
    std::reverse(ALL(arev));

    int64_t ans = TYPEMAX(int64_t);

    auto pre_a = calc(a);
    auto pre_arev = calc(arev);
        
        
    for (int last_neg = -1; last_neg < n; ++last_neg) {
        int64_t ths = 0;

        // [0; last_neg] --[rev]--> [n - 1 - last_neg, n - 1].
        if (last_neg != -1) {
            ths += (1 + last_neg) + 2 * pre_arev[n - 1 - last_neg][0];
        }
        
        if (last_neg != n - 1) {
            ths += 2 * pre_a[last_neg + 1][0];
        }

        ans = min(ans, ths);
    }

    cout << ans << "\n";
    
    return 0;
}


Measured memory_bytes: 120304
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		int stack[] = new int[1000];

		Scanner scanner = new Scanner(System.in);
		int counta = 0;
		String input[] = (scanner.nextLine()).split(" ");

		for (int i = 0; i < input.length; i++) {
			if (input[i].equals("*")) {
				stack[counta - 2] = stack[counta - 2] * stack[counta - 1];
				counta--;
			} else if (input[i].equals("+")) {
				stack[counta - 2] = stack[counta - 2] + stack[counta - 1];
				counta--;

			} else if (input[i].equals("-")) {
				stack[counta - 2] = stack[counta - 2] - stack[counta - 1];
				counta--;

			} else {
				stack[counta] = Integer.parseInt(input[i]);
				counta++;
			}
		}
		System.out.println(stack[counta - 1]);
	}

}


Measured memory_bytes: 26240
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int n,m;
char c[1010][1010];
int s1,s2,t1,t2;
int d[1010][1010];
deque< pair<int,PII> > dq;
int d1[4]={0,0,1,-1},d2[4]={1,-1,0,0};
int main()
{
	scanf("%d%d",&n,&m);
	scanf("%d%d",&s1,&s2);
	scanf("%d%d",&t1,&t2);
	for (int i=0;i<n;i++) scanf("%s",&c[i]);
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++) d[i][j]=1e9;
	s1--,s2--,t1--,t2--;
	d[s1][s2]=0;
	dq.push_back(MP(0,MP(s1,s2)));
	while (!dq.empty())
	{
		int st=dq.front().Fst;
		int x=dq.front().Snd.Fst,y=dq.front().Snd.Snd;
		//cout <<x<<" "<<y<<" "<<st<<" ######\n";
		dq.pop_front();
		if (st!=d[x][y]) continue;
		for (int i=-2;i<=2;i++)
		{
			for (int j=-2;j<=2;j++)
			{
				if (i==0 && j==0) continue;
				int xi=x+i,yi=y+j;
				if (xi<0 || yi<0 || xi>=n || yi>=m || c[xi][yi]=='#') continue;
				if (d[xi][yi]<=st+1) continue;
				d[xi][yi]=st+1;
				dq.push_back(MP(d[xi][yi],MP(xi,yi)));
				//cout <<xi<<" "<<yi<<"##\n";
			}
		}
		for (int i=0;i<4;i++)
		{
			int xi=x+d1[i],yi=y+d2[i]; 
			if (xi<0 || yi<0 || xi>=n || yi>=m || c[xi][yi]=='#') continue;
			if (d[xi][yi]<=st) continue;
			d[xi][yi]=st;
			dq.push_front(MP(d[xi][yi],MP(xi,yi)));
			//cout <<xi<<" "<<yi<<"##\n";
		}
	}
	//cout <<"OP\n";
	if (d[t1][t2]==1e9) printf("-1");
	else printf("%d",d[t1][t2]);
	return 0;
}

Measured memory_bytes: 8832
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = int(input())
a = input()
ans = 0
for i in range(1000):
    i = str(i).zfill(3)
    x = a.find(i[0])
    y = a.find(i[1], x+1)
    z = a.find(i[2], y+1)
    if x != -1 and y != -1 and z != -1:
        ans += 1
print(ans)


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

using namespace std;
using int64 = long long;

int main() {
    int n,m,r;
    cin >> n >> m >> r;
    vector<int> rs(r);
    for (int i=0; i<r; i++) cin >> rs[i];

    vector<vector<int64>> d(n+1, vector<int64>(n+1, INT_MAX));

    for (int i=0; i<m; i++) {
        int a,b,c;
        cin >> a >> b >> c;
        d[a][b] = c;
        d[b][a] = c;
    }

    for (int k=1; k<=n; k++) {
        for (int i=1; i<=n; i++) {
            for (int j=1; j<=n; j++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    sort(rs.begin(), rs.end());

    int64 ans = LONG_MAX;
    do {
        int64 dist = 0;
        for (int i=0; i<r-1; i++) {
            dist += d[rs[i]][rs[i+1]];
        }
        ans = min(ans, dist);
    } while(next_permutation(rs.begin(), rs.end()));

    cout << ans << endl;
}

Measured memory_bytes: 640
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define rep(i,a,b) for(int i=(a),i##_end_=(b);i<=i##_end_;i++)
#define dwn(i,a,b) for(int i=(a),i##_end_=(b);i>=i##_end_;i--)
using namespace std;
typedef long long ll;
const int maxn=1010;
const int mod=1000000007;
struct Matrix {
	int A[2][2];
	Matrix operator * (const Matrix& b) const {
		Matrix c;
		rep(i,0,1) rep(j,0,1) {
			ll res=0;
			rep(k,0,1) res+=(ll)A[i][k]*b.A[k][j];
			c.A[i][j]=res%mod;
		}
		return c;
	}
}T;
int qpow(int n,ll m) {
	int ans=1;
	for(;m;m>>=1,n=(ll)n*n%mod) if(m&1) ans=(ll)n*ans%mod;
	return ans;
}
int n,m;
ll k;
char M[maxn][maxn];
int main() {
	scanf("%d%d%lld",&n,&m,&k);k--;
	rep(i,1,n) scanf("%s",M[i]+1);
	int c=0,cx=0,cy=0,tx=0,ty=0;
	rep(i,1,n) rep(j,1,m) if(M[i][j]=='#') c++;
	rep(i,1,n) {
		if(M[i][1]=='#'&&M[i][m]=='#') cx++;
		rep(j,1,m-1) if(M[i][j]=='#'&&M[i][j+1]=='#') tx++;
	}
	rep(j,1,m) {
		if(M[1][j]=='#'&&M[n][j]=='#') cy++;
		rep(i,1,n-1) if(M[i][j]=='#'&&M[i+1][j]=='#') ty++;
	}
	if(cx&&cy) puts("1");
	else if((!cx)&&(!cy)) printf("%d\n",qpow(c,k));
	else {
		if(!cx) swap(cx,cy),swap(tx,ty);
		T.A[0][0]=c;T.A[0][1]=0;
		T.A[1][0]=tx;T.A[1][1]=cx;
		Matrix ans;
		ans.A[0][0]=ans.A[1][1]=1;
		ans.A[1][0]=ans.A[0][1]=0;
		for(;k;k>>=1,T=T*T) if(k&1) ans=ans*T;
		int v=ans.A[0][0],e=ans.A[1][0];
		printf("%d\n",(v-e+mod)%mod);
	}
	return 0;
}


Measured memory_bytes: 1152