akhauriyash/Code-Regression huggingface dataset:
let () =
    let main () =
        let h, w = Scanf.sscanf (read_line ()) "%d %d" (fun h w -> h, w) in
        let s = Array.init h (fun _ -> read_line ()) in
        let mat1 = Array.make_matrix h w 0 in
        let mat2 = Array.make_matrix h w 0 in
        let mat3 = Array.make_matrix h w 0 in
        let mat4 = Array.make_matrix h w 0 in
        for y = 0 to h - 1 do
            let rec loop x acc =
                if x < w then (
                    mat1.(y).(x) <- acc;
                    let acc = if s.(y).[x] = '#' then 0 else acc + 1 in
                    loop (x + 1) acc
                )
            in
            loop 0 0;
            let rec loop x acc =
                if x >= 0 then (
                    mat2.(y).(x) <- acc;
                    let acc = if s.(y).[x] = '#' then 0 else acc + 1 in
                    loop (x - 1) acc
                )
            in
            loop (w - 1) 0;
        done;
        for x = 0 to w - 1 do
            let rec loop y acc =
                if y < h then (
                    mat3.(y).(x) <- acc;
                    let acc = if s.(y).[x] = '#' then 0 else acc + 1 in
                    loop (y + 1) acc
                )
            in
            loop 0 0;
            let rec loop y acc =
                if y >= 0 then (
                    mat4.(y).(x) <- acc;
                    let acc = if s.(y).[x] = '#' then 0 else acc + 1 in
                    loop (y - 1) acc
                )
            in
            loop (h - 1) 0;
        done;
        let rec loop_y y acc =
            let rec loop_x x acc =
                if x = w then acc else
                    if s.(y).[x] = '#' then loop_x (x + 1) acc else
                        let acc = max acc (1 + mat1.(y).(x) + mat2.(y).(x) + mat3.(y).(x) + mat4.(y).(x)) in
                        loop_x (x + 1) acc
            in
            if y = h then acc else loop_y (y + 1) (loop_x 0 acc)
        in
        Printf.printf "%d\n" (loop_y 0 0)
    in
    main ()

Measured memory_bytes: 134772
#include <iostream>

void solve() {
    int ans = 0, cont = 0;

    for (int i = 0; i < 3; ++i) {
        char c;
        std::cin >> c;

        if (c == 'R') {
            ++cont;
        } else {
            cont = 0;
        }
        ans = std::max(ans, cont);
    }

    std::cout << ans << "\n";
}

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

    solve();

    return 0;
}


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

int main()
{
	long N,i,temp,ans=0;
	vector<long> A,B,C;
	cin>>N;
	for(i=0;i<N;i++){
		cin>>temp;
		A.push_back(temp);
	}
	for(i=0;i<N;i++){
		cin>>temp;
		B.push_back(temp);
	}
	for(i=0;i<N-1;i++){
		cin>>temp;
		C.push_back(temp);
	}
	ans+=B.at(A.at(0)-1);
	for(i=1;i<N;i++){
		ans+=B.at(A.at(i)-1);
		if(A.at(i)==A.at(i-1)+1){
			ans+=C.at(A.at(i-1)-1);
		}
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        boolean[] bool = new boolean[s.length()];
        for (int i=0;i<s.length();i++){
            if (s.charAt(i)=='W'){
                bool[i]=true;
            }else{
                bool[i]=false;
            }
        }
        int count = 0;
        for (int i=0;i<s.length()-1;i++){
            if (bool[i]!=bool[i+1])count++;
        }
        System.out.println(count);
    }

}

Measured memory_bytes: 25200
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int N;
  cin>>N;
  vector<int>vec(N);
  for(int l=0;l<N;l++){
    cin>>vec.at(l);
  }
  int sum=0;
 if(N>=3){
  for(int i=0;i<N-2;i++){
    for(int j=i;j<N-1;j++){
      for(int k=j;k<N;k++){
        if(vec.at(i)!=vec.at(k)&&vec.at(k)!=vec.at(j)&&vec.at(j)!=vec.at(i)&&
           vec.at(i)+vec.at(j)>vec.at(k)&&vec.at(i)+vec.at(k)>vec.at(j)&&
           vec.at(k)+vec.at(j)>vec.at(i)){
          sum++;
        }
      }
    }
  }
 }
  cout<<sum<<endl;
}

Measured memory_bytes: 3624
#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
lint mo=1000000007;
lint zyo(lint x,lint y){
    lint ret=1,a=x;
    while(y>0){
    	if(y%2==1) ret=(ret*a)%mo;
    	a=(a*a)%mo;y/=2;
    }
    return ret;
}
vector<lint> yak;
lint dp[114514];
int main()
{
	int n,k;lint out=0;
	cin>>n>>k;
	for(int i=1;i*i<=n;i++){
		if(n%i==0){
			yak.pb(i);
			if(i*i<n) yak.pb(n/i);
		}
	}
	sort(All(yak));
	int m=yak.size();
	rep(i,m){
		dp[i]=zyo(k,((yak[i]+1)/2));
		rep(j,i){
			if(yak[i]%yak[j]>0) continue;
			dp[i]+=mo-dp[j];
		}
		dp[i]%=mo;
	}
	rep(i,m){
		if(yak[i]%2>0){
			out+=dp[i]*yak[i];
		}
		else{
			out+=dp[i]*yak[i]/2;
		}
		out%=mo;
	}
	cout<<out<<endl;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;


struct box{
	int x,y;
};

int32_t main()
{
	IOS
	string s1,s2;cin>>s1>>s2;
	int n1=s1.length();
	int n2=s2.length();
	
	s1='$'+s1;
	s2='$'+s2;
	
	vector<vector<int>>lcs(n1+1,vector<int>(n2+1));
	vector<vector<box>>par(n1+1,vector<box>(n2+1));
	
	for(int i=1;i<=n1;i++)
	for(int j=1;j<=n2;j++)
	{
		if( s1[i]==s2[j] )
		{
			 lcs[i][j] = lcs[i-1][j-1] + 1;
			 par[i][j] = {i-1,j-1};
		}
		else
		{
			if( lcs[i-1][j] > lcs[i][j-1])
			{
				 lcs[i][j] = lcs[i-1][j];
				 par[i][j] = {i-1,j};
		    }
		    else
		    {
				
				 lcs[i][j] = lcs[i][j-1];
				 par[i][j] = {i,j-1};
		    }
		}
	}
	
	string ans;
	int x = n1;
	int y = n2;
	
	//cout << lcs[x][y] << endl;
	while( x || y )
	{
		int xp = par[x][y].x;
		int yp = par[x][y].y;
		
		if( xp < x && yp < y)
		{
			ans+=s1[x];
		}
		
		x=xp;
		y=yp;
	}
	
	reverse(ans.begin(),ans.end());
	cout << ans << endl;
	
	
	
}


Measured memory_bytes: 211584
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>

using namespace std;

int main(void){
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        int a;
        cin >> a;
        if(a % 2 == 1){
            cout << "first" << endl;
            return 0;
        }
    }
    cout << "second" << endl;
    return 0;
}

Measured memory_bytes: 256
N, K = map(int, input().split(' '))
R, S, P = map(int, input().split(' '))
T = list(input())
score = 0
A = T
for i in range(N-K):
  if T[i] == T[i+K]:
    A[i+K] = 'a'

for i in range(N):
  if A[i] == 'r':
    score += P
  elif A[i] == 's':
    score += R
  elif A[i] == 'p':
    score += S
print(score)

Measured memory_bytes: 4084
#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = m; i < (n); i++)
#define RFOR(i, m, n) for (int i = (m - 1); i >= 0; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;

int main() {
  int n, k, count = 0;
  cin >> n >> k;
  REP(i, n) {
    int h;
    cin >> h;
    if (h >= k) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) REP(i, 0, n)
#define REP(i, l, r) for (int i = l; i < r; i++)
using namespace std;
typedef pair<int, int> P;
struct edge {
	int to, cost;
};
class dijkstra {
  private:
	int V;
	int d[100002];
	priority_queue<P, vector<P>, greater<>> que;

  public:
    dijkstra(int v, int s, vector<edge> G[]) {
        V = v;
        update(s, G);
    }

    void update(int s, vector<edge> G[]) {
        fill(d, d + V + 1, 100000000000ll);
        d[s] = 0;
        que.push(make_pair(0, s));
        while (!que.empty()) {
            P p = que.top();
            que.pop();
            int v = p.second;
            if (d[v] < p.first)
                continue;
            rep(i, G[v].size()) {
                edge e = G[v][i];
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    que.push(make_pair(d[e.to], e.to));
                }
            }
        }
    }

	int find(int e) {
		return d[e];
	}
};

int gcd(int a, int b) {
	if (a % b == 0)
		return b;
	else
		return gcd(b, a % b);
}

int lcm(int a, int b) {
	return a * b / gcd(a, b);
}

bool prime(int a) {
	if (a == 1)
		return false;
	for (int i = 2; i * i <= a; i++) {
		if (a % i == 0)
			return false;
	}
	return true;
}

class Union_Find {
  private:
	vector<int> t, s;

  public:
	Union_Find(int max_length) {
		rep(i, max_length + 1) {
			t.push_back(i);
			s.push_back(1);
		}
	}

	void Union(int x, int y) {
		if (same(x, y))
			return;
		int tx = Find(x), ty = Find(y);
		if (s[tx] < s[ty]) {
			s[ty] += s[tx];
			t[tx] = ty;
		}
		else if (s[tx] > s[ty]) {
			s[tx] += s[ty];
			t[ty] = tx;
		}
		else if (tx > ty) {
			t[tx] = ty;
			s[ty] += s[tx];
		}
		else {
			t[ty] = tx;
			s[tx] += s[ty];
		}
	}

	int Find(int n) {
		if (t[n] == n)
			return n;
		else
			return t[n] = Find(t[n]);
	}

	bool same(int x, int y) {
		return Find(x) == Find(y);
	}

	int get_Size(int a) {
		return s[a];
	}
};

vector<int> primeFactorization(int n) {
	vector<int> re;
	int cnt = 2;
	while (n != 1) {
		if (n % cnt != 0) {
			cnt++;
			continue;
		}
		n /= cnt;
		re.push_back(cnt);
	}
	return re;
}

signed main() {
    int x,a;
    cin>>x>>a;
    if(x<a)cout<<0<<endl;
    else cout<<10<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long int lli;
typedef unsigned long long int ulli;
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define sortv(v) sort(v.begin(),v.end());
#define sorta(a,n) sort(a,a+n);
#define arrayin(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define arrayout(a,n) for(int i=0;i<n;i++) cout<<" ";cout<<"\n";
#define INF INT_MAX //Infinity
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define MOD 1000000007
#define fi first
#define se second
#define vi vector<int>
#define ii pair<int,int>
#define vii vector<pair<int,int>>
#define viii vector<tuple<int,int,int>>
#define mii map<int,int>
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define nl "\n"
ulli poW(lli x,ulli y)
{
	//this function only returns correct values upto the order of ~10^18
	//use pow of math to obtain greater answers
	lli temp;
	if(y==0)
		return 1;
	temp=poW(x,y/2);
	if(y%2==0)
		return temp*temp;
	else return x*temp*temp;
}

int bSearch(int low,int high,int a[],int sh)
{
	//function for searching till where is a sh<=
	while(low<high)
	{
		int mid=low+(high-low+1)/2;
		if(a[mid]>sh)
			high=mid-1;
		if(a[mid]<=sh)
			low=mid;
	}
	return low;
}

int main()
{
    fio;
	int n;
	cin>>n;
	string s;
	cin>>s;
	int counter1=0,counter2=0;
	REP(i,n)
	{
		if(s.at(i)==')')
		{
			if(counter2>0)
				counter2--;
			else counter1++;
		}
		else counter2++;
	}
	REP(i,counter1)
	{
		s="("+s;
	}
	REP(i,counter2)
	{
		s=s+")";
	}
	cout<<s<<nl;
}

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

const int N = 100500;
int cnt[N];
bool may[N];

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    may[0] = true;
    for (int i = 0; i < n; i++)
        cnt[i] = 1;
    for (int i = 0; i < m; i++) {
        int x, y;
        scanf("%d %d", &x, &y);
        --x, --y;
        if (may[x])
            may[y] = true;
        --cnt[x], ++cnt[y];
        if (cnt[x] == 0)
            may[x] = false;
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += (int)may[i];
    printf("%d\n", ans);
}


Measured memory_bytes: 640
def calctime(w,dist):
    time=dist/(2000/(70.0+w))
    return time

n=int(raw_input())
s={}
d={}
v={}
for i in xrange(n):
    S,D,V=map(int,raw_input().split())
    s[i]=S
    d[i]=D
    v[i]=V

dist=[[None]*n for _ in xrange(n)]
for i in xrange(n):
    for j in xrange(n):
        if i==j:
            dist[i][j]=float('inf')
        else:
            dist[i][j]=abs(d[i]-d[j])

w=[float('inf')]*(1<<n)
time=[[float('inf')]*n for _ in xrange(1<<n)]
last=[[0]*n for _ in xrange(1<<n)]
for i in xrange(n):
    time[1<<i][i]=0
    w[1<<i]=v[i]*20

for i in xrange(1,1<<n):
    for j in xrange(n):
        if w[i]==float('inf'):continue
        for k in xrange(n):
            if not i>>k&1:
                nexti=i|(1<<k)
                w[nexti]=w[i]+v[k]*20
                if time[nexti][k]>calctime(w[i],dist[j][k])+time[i][j]:
                    time[nexti][k]=calctime(w[i],dist[j][k])+time[i][j]
                    last[nexti][k]=j

ans=[]
now=(1<<n)-1
last_index=time[now].index(min(time[now]))
ans.append(s[last_index])
while now!=0:
    nx=last[now][last_index]
    ans.append(s[nx])
    now=now^(1<<last_index)
    last_index=nx
del ans[-1]
ans.reverse()
print(' '.join(map(str,ans)))

Measured memory_bytes: 27384
N = int(input())

S = input()

R = 0
G = 0
B = 0

for i in S:
    if i == "R":
        R += 1
    elif i == "G":
        G += 1
    else:
        B += 1

ans = R*G*B

now = [0,0,0]
for i in range(1,N):

    for j in range(N):

        if j+2*i >= N:
            break

        now = [0,0,0]

        for l in range(3):
            if S[j+l*i] == "R":
                now[0] += 1
            elif S[j+l*i] == "G":
                now[1] += 1
            else:
                now[2] += 1

        if now[0] == 1 and now[1] == 1 and now[2] == 1:
            ans -= 1

print (ans)


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

int main(){
int AB,BC,AC,D;
  
  cin>>D;
  switch(D){
    case 22:
      cout<<"Christmas Eve Eve Eve";
            break;
  case 23:
      cout<<"Christmas Eve Eve";
            break;
  case 24:
      cout<<"Christmas Eve";
            break;
  case 25:
      cout<<"Christmas";
            break;
    default:
       break;
}
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>

long calcNumOfCombination(long n, long r){
        long num = 1;
        for(long i = 1; i <= r; i++){
            num = num * (n - i + 1) / i;
        }
        return num;
    }

void solve(int N, std::vector<std::string> s) 
{
  for(auto& i: s)
  {
    std::sort(i.begin(), i.end());
  }
  std::sort(s.begin(), s.end());
  long count = 0;
  std::vector<long> countv;
  for (int i = 1; i < N; i++)
  {
    if(s[i-1]==s[i])
    {
      count++;
      if(i==N-1)
      {
        countv.push_back(count);
      }
    }
    else if(count!=0) 
    {
      countv.push_back(count);
      count=0;
    }
    
  }
  long result = 0;
  for(auto& i: countv)
  {
    result += calcNumOfCombination(i+1, 2);
  }
  std::cout << result << std::endl;
}
int main(void) {
  int N;
  std::cin >> N;
  std::vector<std::string> s(N);
  for(auto i0 = 0; i0 < N; ++i0)
  {
    std::cin >> s[i0];
  }
  solve(N,s);
  return 0;
}


Measured memory_bytes: 13056
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Main{
	public static void main(String[] args) {

		BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
		String str="";
		StringTokenizer stk;
		int[] k1=new int[3];
		int i=0;
		try {
			while(true) {

				str = bfr.readLine();

				if(str==null) {
					System.exit(0);
				}

				stk=new StringTokenizer(str," ");

				while(stk.hasMoreTokens()) {
					k1[i]=Integer.parseInt(stk.nextToken());
					i++;
				}
				i=0;

				int kariiregaku=k1[2]-k1[0];
				if(kariiregaku<0) {
					kariiregaku=0;
				}

				if(k1[1]>=kariiregaku) {
					System.out.println(kariiregaku);
				}
				else {
					System.out.println("NA");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}



Measured memory_bytes: 24488
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include<stack>

using namespace std;
int t[11];
stack<int> a;
stack<int> b;
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
       while(!a.empty())
        a.pop();
       while(!b.empty())
        b.pop();
        int flag=0;
        memset(t,0,sizeof(0));
        for(int i=0;i<10;i++)
        {
            scanf("%d",&t[i]);
        }
       // sort(t,t+10,cmp);
        a.push(t[0]);
        b.push(0);
        for(int i=1;i<10;i++)
        {
            if(t[i]>a.top()&&t[i]<=b.top())
                a.push(t[i]);
            else if(t[i]<=a.top()&&t[i]>b.top())
                b.push(t[i]);
             else if(t[i]>a.top()&&t[i]>b.top())
             {
                 if(a.top()>b.top())
                  a.push(t[i]);
                 else
                    b.push(t[i]);
                    
             }
               
                else if(t[i]<=a.top()&&t[i]<=b.top())
              {
                  flag=1;
                  break;
              }
        }
        if(flag)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;



    }
}

Measured memory_bytes: 3140
program atcoder_contest_scheduling
  implicit none
  integer, parameter :: maxday = 365, maxtype = 26
  integer :: d, c(maxtype) = 0, s(maxtype, maxday) = 0, t(maxday) = 0, u(maxday)
  integer :: m, n, p, i
  real(8) :: t1, t2
  call cpu_time(t1)
  call random_seed_clock()
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  t = [(maxloc(s(:, i), 1), i = 1, d)]
  m = score()
  u = t
  do
    p = next_int(maxday)
    do i = 1, maxtype
      t(p) = i
      n = score()
      if (m < n) then
        m = n
        u(p) = i
      else
        t(p) = u(p)
      end if
      call cpu_time(t2)
      if (t2 - t1 > 1.979d0) goto 100
    end do
  end do
  100 write(*,'(i0)') (u(i), i = 1, d)
contains
  integer function score() result(res)
    integer :: last(maxtype), i
    last = 0
    res = 0
    do i = 1, d
      last(t(i)) = i
      res = res + s(t(i), i) + sum(c * (last - i))
    end do
  end
  subroutine random_seed_clock()
    integer :: nseed, clock
    integer, allocatable :: seed(:)
    call system_clock(clock)
    call random_seed(size = nseed)
    allocate(seed(nseed))
    seed = clock
    call random_seed(put = seed)
    deallocate(seed)
  end
  integer function next_int(m) result(res)
    integer, intent(in)  :: m
    real(8) :: r
    call random_number(r)
    res = int(m * r) + 1
  end
end program atcoder_contest_scheduling

Measured memory_bytes: 3032
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMX LONG_LONG_MAX
int main()
{
  ll n;
  cin>>n;
  vector< vector< ll > >dp(n, vector< ll >(3));
  for(ll i=0;i<n;i++)
  {
    ll a, b, c;
    cin>>a>>b>>c;
    if(i == 0)
    {
      dp[i][0] = a;
      dp[i][1] = b;
      dp[i][2] = c; 
    }
    else
    {
      dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a;
      dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + b;
      dp[i][2] = max(dp[i-1][1], dp[i-1][0]) + c;
    }
  }
  cout << max({dp[n-1][0], dp[n-1][1], dp[n-1][2]});
  return 0;
}

Measured memory_bytes: 5760
/*
整数 X を十進法で表したときの各桁の数字の和を f(X) としたとき、
X が f(X) で割り切れる場合、
X はハーシャッド数です。
*/
#include <iostream>
using namespace std;

// 各桁の和を計算する関数
int findSumOfDigits(int n) {
  int sum = 0;
  while (n > 0) { // n が 0 になるまで
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main() {
  int N;
  cin >> N;
  
  int sum = findSumOfDigits(N); // N の各桁の和
  if (N%sum==0) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int n;
const int SIZE=1<<19;
P seg[SIZE*2];
struct segtree
{
	segtree()
	{
		for(int i=0;i<SIZE*2;i++)seg[i]=P(0,0);
	}
	void update(int k,int x)
	{
		k+=SIZE-1;
		seg[k].fi=x;
		seg[k].sec=k-(SIZE-1);
		while(k>0)
		{
			k=(k-1)/2;
			seg[k]=max(seg[k*2+1],seg[k*2+2]);
		}
	}
	int get_max_value()
	{
		return seg[0].fi;
	}
	int get_max_id()
	{
		return seg[0].sec;
	}
};
struct bug{int a,b;};
bool comp(bug p,bug q)
{
	if(p.b!=q.b)return p.b > q.b;
	else return p.a < q.a;
}
bug foo[300100];
bool check(int x)
{
	if(x>n)return false;
	ll sum=0ll;
	ll limit=foo[x-1].b;
	segtree seg;
	for(int i=0;i<x;i++)sum+=foo[i].a;
	for(int i=0;i<x;i++)seg.update(i,foo[i].a);
	//cout << limit << ' ' << sum << ' ' << x << endl;
	if(limit*x>=sum)return true;
	for(int i=x;i<n;i++)
	{
		if(seg.get_max_value()<=foo[i].a)continue;
		limit=foo[i].b;
		int p=seg.get_max_id();
		seg.update(p,0);
		seg.update(i,foo[i].a);
		sum-=foo[p].a;
		sum+=foo[i].a;
		//cout << limit << ' ' << sum << ' ' << x << endl;
		if(limit*x>=sum)return true;
	}
	return false;
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%d %d",&foo[i].a,&foo[i].b);
	sort(foo,foo+n,comp);
	int l=0,r=n+1;
	while(r-l>1)
	{
		int mid=(l+r)/2;
		if(check(mid))l=mid;
		else r=mid;
	}
	printf("%d\n",l);
	return 0;
}

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

#define ll       long long
#define LL       long long
#define ull      unsigned ll
#define pii      pair<int, int>
#define pll      pair<ll, ll>
#define mp       make_pair
#define ff       first
#define ss       second
#define sz(s)    (int) s.size()
#define all(s)   s.begin(), s.end()

template<typename T> inline void smin(T &a, T b) {
    a = a < b ? a : b;
}
template<typename T> inline void smax(T &a, T b) {
    a = a > b ? a : b;
}

template<typename T> inline void Int(T &n) {
    n = 0;
    int f = 1;
    register int ch = getchar();
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    for(; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';
    n *= f;
}
template<typename T, typename TT> inline void Int(T &n, TT &m) {
    Int(n), Int(m);
}
template<typename T, typename TT, typename TTT> inline void Int(T &n, TT &m, TTT &l) {
    Int(n), Int(m, l);
}

#define error(args...) { string _s = #args; replace(all(_s), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {
    cout << endl;
}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << ", ";
    err(++it, args...);
}

const int mod = (int) 1e9 + 7;

int add(int a, int b) {
    a += b;
    if(a >= mod) a -= mod;
    return a;
}
int sub(int a, int b) {
    a -= b;
    if(a <= 0) a += mod;
    return a;
}
int mul(int a, int b) {
    return (ll) a * b % mod;
}

const int inf = (int) 1e9 + 5;
const ll  Inf = (ll) 2e18 + 5;
const int   N = (int) 2e5 + 5;




int solve() {
    int n, w;
    Int(n, w);
    vector<ll> dp(N, Inf);
    dp[0] = 0;
    for(int i = 1; i <= n; i++) {
        ll wt, val; Int(wt, val);
        for(int j = N - 1; j >= 0; j--) {
            if(j - val >= 0) smin(dp[j], dp[j - val] + wt);
        }
    }
    int ans = 0;
    for(int i = 1; i < N; i++) {
        if(dp[i] <= w) ans = i;
    }
    printf("%d\n", ans);
    return 0;
}

int main() {
    int test = 1, tc = 0;
    //Int(test);
    while(test--) {
        //printf("Case %d: ", ++tc);
        solve();
    }
    return 0;
}




Measured memory_bytes: 1792
n,m=map(int,input().split())
a,c=[],0
for i in range(m):
    a.append(list(map(int,input().split())))
p=list(map(int,input().split()))
for i in range(2**n):
    i,b='0'*(n-len(bin(i))+2)+bin(i)[2:],0
    for j in range(m):
        f=0
        for k in range(a[j][0]):
            f+=int(i[a[j][k+1]-1])
        if f%2==p[j]:
            b+=1
    if b==m:
        c+=1
print(c)

Measured memory_bytes: 3064
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; 

int main(){
    fastio
    string s ; 
    cin >> s ; 
    int n = (int) s.size() ; 
    int ans = 0 ; 
    int cnt = 0 ; 
    for(int i=0;i<n;i++){
    	if ( i & 1 ){
    		if ( s[i] == '1' ) cnt++ ; 
    	}
    	else {
    		if ( s[i] == '0' ) cnt++ ; 
    	}
    }
    ans = cnt ; 
    cnt = 0 ; 
    for(int i=0;i<n;i++){
    	if ( i & 1 ){
    		if ( s[i] == '0' ) cnt++ ; 
    	}
    	else {
    		if ( s[i] == '1' ) cnt++ ; 
    	}
    }
    cout << min(ans,cnt) << endl;
    return 0 ;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i,n) for(int i=n; i>-1; --i)
#define ALL(a) (a).begin(),(a).end()
#define INIT(a,n,x); REP(i,(n)){ (a)[i]=(x); }
#define CINA(a,n); REP(i,(n)){ cin >> (a)[i]; }
#define INIT2(a,n,m,x); REP(i,(n)){ REP(j,(m)){(a)[i][j]=(x);} }
#define CINA2(a,n,m); REP(i,(n)){ REP(j,(m)){cin >> (a)[i][j];} }
#define Liny "Yes\n"
#define Linn "No\n"
#define LINY "YES\n"
#define LINN "NO\n"

#define umap unordered_map
//cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x){ if(x<10){return 1;} else{return keta(x/10) + 1;}}
int keta_wa(ll x){ if(x<10){return x;} else{return keta_wa(x/10) + x%10;} }
int ctoi(char c){ return ( (c>='0' && c<='9')? c - '0': 0 );}
ll sum(ll a[],ll N){ return accumulate(a,a+N,0LL);}
ll gcd(ll a,ll b){if(a<b)swap(a,b); return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){if(a<b){swap(a,b);} return a/gcd(a,b)*b;}

int main(){
  ll K,N; cin>>K>>N;
  ll a[N]; CINA(a,N);
  ll r = (K-a[N-1])+a[0];
  REP(i,N-1){
    ll temp = a[i+1]-a[i];
    r = max(temp,r);
  }
  cout << K-r <<"\n";
}

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

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define MX              200005
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define ull unsigned long long
#define ll long long
void codeforces()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.in", "w", stdout);
    #endif
}

int32_t main()
{
    //codeforces();
    // int t;cin>>t;
    // while(t--) {
        
    // }
    int n;cin>>n;
    vi b(n-1);
    for(int i=0;i<n-1;i++)cin>>b[i];
    vi a(n);
    a[0]=b[0];
    a[1]=b[0];
    for(int i=1;i<n-1;i++){
        if(a[i]>b[i]){
            a[i]=b[i];
        }
        a[i+1]=b[i];
    }
    
    // for(int i=0;i<n;i++)cout<<a[i]<<" ";
    //     cout<<endl;
    int sum=accumulate(a.begin(),a.end(),0);
    cout<<sum<<endl;
    return 0;
}

Measured memory_bytes: 256
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}


fn main() {
    input! {
        a: i32,
        b: i32,
    }

    let x = (a + b) / 2;
    let y = (a + b) % 2;

    println!("{}", x+y);

}


Measured memory_bytes: 4352
require 'prime'

N = gets.to_i

ans = N - 1
f = Prime.prime_division(N)

dfs = lambda { |d, i|
  0.upto(f[i][1]) do |n|
    x = d
    x *= f[i][0] ** n if n != 0
    if i == f.size - 1
      a = (x - 1) + (N / x - 1)
      ans = a if a < ans
    else 
      dfs.call(x, i + 1)
    end
  end
}

dfs.call(1, 0)
puts ans 


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

int main()
{
	map<string ,string> a;
	a["Sunny"] = "Cloudy";
	a["Cloudy"] = "Rainy";
	a["Rainy"] = "Sunny";
	
	string s;
	cin >> s;
	
	cout << a[s] << endl;
	return 0;
	
}

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

typedef long long ll;

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define tt int t; cin>>t; while(t--)
#define nl cout<<"\n";
#define sp cout<<" ";
#define rep(i, a, b) for(long long i=a; i<b; i++)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
double PI = 4*atan(1);

int main()
{
    optimize();

    int n, h, t, s= 0;

    cin>> n>> h;

    for(int i=0; i<n; i++)
    {
        cin>> t;

        if(t>= h)
            s++;
    }

    cout << s;
    nl;


    return 0;
}




Measured memory_bytes: 3532
#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 euqlid (int a, int b){
  int temp;
  temp = a % b;
  if(temp == 0)return b;
  return euqlid(b,temp);
}
ll conbination(ll a,ll b){
  ll u = 1;
  ll d = 1;
  while(b != 0){
    u *= a;
    d *= b;
    a--;
    b--;
  }
  return u / d;
}
int fact(int n){
  if(n == 1)return 1;
  return n * fact(n-1);
  }

int main(){
  string s;
  cin >> s;
  string temp[3] = {"Sunny","Cloudy","Rainy"};
  rep(i,3){
    if(s == temp[i]){
      cout << temp[(i+1) % 3] << endl;;
      return 0;
    }
  }
}


Measured memory_bytes: 256
/*	ex3_3
	suzuto	*/

#include <stdio.h>

int main(void) {
  int num, k_law , k_exp, order; /*変数の宣言*/
  scanf("%d %d", &num, &k_law); /*整数Nを入力*/

  /*Nのk進数表記における桁数mに対し k^(m - 1) <= N < k^mが成り立つため、
  k^(m - 1) <= Nが成り立つ最大のmを探す*/
  k_exp = k_law; /*k_expの初期化*/
  order = 1; /*orderの初期化*/

  /*k^(m - 1) > Nとなるまで捜査を続ける*/
  while(num >= k_exp){
    k_exp *= k_law; /*k_expの更新*/
    order++; /*orderの更新*/
  }

  printf("%d\n", order); /*Nの桁数を出力*/

  return 0;
}


Measured memory_bytes: 128
/*
 *Author : zsenshi
 DP 
 */
#include <bits/stdc++.h> //Only for gcc compilers
#define mt make_tuple
#define eb emplace_back
#define pb push_back
#define dg(x) cout << #x << ":"<<x<<endl
#define dg2(x,y) cout<<#x<<':'<<x<<' '<<#y<<':'<<y<<endl
#define dg3(x,y,z) cout<<#x<<':'<<x<<' '<<#y<<':'<<y<<' '<<#z<<':'<<z<<endl
#define dg4(x,y,z,zz) cout<<#x<<':'<<x<<' '<<#y<<':'<<y<<' '<<#z<<':'<<z<<' '<<#zz<<':'<<zz<<endl
#define rep(i,begin,end) for(__typeof(end) i = (begin)-((begin) > (end)); i != (end) - ((begin) > (end)); i += 1-2*((begin) > (end)))
using namespace std;
typedef vector<vector<int>> vvi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<pii>> vvpi;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> points(3);
  vector<vector<int>>dp(n,vector<int>(3,0));
  for(int i = 0; i < n; i++){
    cin >> points[0] >> points[1] >> points[2];
    if(i == 0){
      dp[i][0] = max({points[1],points[2]});
      dp[i][1] = max({points[0],points[2]});
      dp[i][2] = max({points[0],points[1]});
    }
    for(int j = 0; j < 3; j++){
      if(i > 0){
        if(j == 0){
          dp[i][j] = max({dp[i][j],dp[i-1][j+1] + points[0],dp[i-1][j+2] + points[0]});
        }
        else if(j == 1){
          dp[i][j] = max({dp[i][j],dp[i-1][j-1] + points[1],dp[i-1][j+1] + points[1]});
        }
        else
          dp[i][j] = max({dp[i][j],dp[i-1][j-1] + points[2],dp[i-1][j-2] + points[2]});
      }
    }
  }
  int answer = 0;
  for(int col = 0; col < 3; col++){
    answer = max(answer,dp[n-1][col]);
  }
  cout << answer << "\n";
}


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

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


int32_t main(){
	int n;
	cin >> n;
	bool check=false;
	vector<int> v(n);
	REP(i,n){
		cin >> v[i];
		if(v[i]==1){
			check=true;
		}
	}
	if(!check){
		cout << -1;
	}
	else{
	int ans=1,cnt=0;
	REP(i,n){
		if(v[i]==ans){
			cnt++;
			ans++;
		}
	}
	cout << n-cnt;
	
	}
}

Measured memory_bytes: 1792
using System;
using System.Linq;

namespace AizuOnlineJudge
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            
            var l = Enumerable.Range(1, n).Where(isOutput);

            Console.WriteLine($" {string.Join(" ", l)}");
        }

        static bool isOutput(int a)
        {
            if (a % 3 == 0) return true;
            while (a > 0)
            {
                if (a % 10 == 3) return true;
                a /= 10;
            }
            return false;
        }
    }
}


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

int main(){
	string s;
	cin >> s;
	int count=0;
	int i;
		if(s[0] == 'R' && s[1] == 'R' && s[2] == 'R') count = 3;
		else if(s[0] == 'R' && s[1] == 'R' && s[2] == 'S') count = 2;
		else if(s[0] == 'R' && s[1] == 'S' && s[2] == 'R') count = 1;
		else if(s[0] == 'S' && s[1] == 'R' && s[2] == 'R') count = 2; 
		else if(s[0] == 'S' && s[1] == 'R' && s[2] == 'S') count = 1;
		else if(s[0] == 'S' && s[1] == 'S' && s[2] == 'S') count = 0;
		else if(s[0] == 'S' && s[1] == 'S' && s[2] == 'R') count = 1;
		else if(s[0] == 'R' && s[1] == 'S' && s[2] == 'S') count = 1;
	cout << count << endl;
	return 0;
}

Measured memory_bytes: 3548
#include <stdio.h>
#include <string.h>
#include <vector>

int main() {
	int N, M;
	scanf("%d%d", &N, &M);
	std::vector<bool> ACed(N + 1, false);
	std::vector<int> WAed(N + 1, 0);
	int AC, WA;
	AC = WA = 0;
	for (int i = 0; i < M; i++) {
		int p;
		char S[2];
		scanf("%d%s", &p, S);
		if (!ACed.at(p)) {
			if (strcmp(S, "AC") == 0) {
				AC++;
				WA += WAed.at(p);
				ACed.at(p) = true;
			} else {
				WAed.at(p)++;
			}
		}
	}
	printf("%d %d\n", AC, WA);
}


Measured memory_bytes: 640
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class ABC_088_D
{	
    static int row = 0;
    static int column = 0;

    static int ans = 0;
    static char[,] map;
    static bool[,] passed; 
    static bool isGoal;
    static int max = 0;
	public static void Main()
	{
        string[] str = Console.ReadLine().Split(' ');
        row = int.Parse(str[0]);
        column = int.Parse(str[1]);
      
        map = new char[row+2,column+2];
        passed = new bool[row+2, column+2];

        for(int i = 0 ; i < row+2 ; i++)
        {
            for(int j = 0 ; j < column+2 ; j++)
            {
                map[i,j] = '#';
            }
        }
        char[] c;
        for(int i = 1 ; i <= row ; i++)
        {
            c = Console.ReadLine().ToCharArray();
            for(int j = 1 ; j <= column ; j++)
            {
                map[i,j] = c[j-1];
                if(c[j-1]=='.')
                {
                    max++;
                }
            }
        }
        BFS(1,1);
        if(isGoal) Console.WriteLine(ans);
        else Console.WriteLine("-1");
    }
    
   
     public static void BFS(int y , int x)
    {
        Queue<int> yPos = new Queue<int>();
        Queue<int> xPos = new Queue<int>();
        Queue<int> cnt = new Queue<int>();

        yPos.Enqueue(y);
        xPos.Enqueue(x);
        cnt.Enqueue(1);

        int nowY, nowX, nowCnt;
        while(yPos.Count > 0 && xPos.Count > 0)
        {
            nowY = yPos.Dequeue();
            nowX = xPos.Dequeue();
            nowCnt = cnt.Dequeue();
            
            if(nowY == row && nowX == column)
            {
                isGoal = true;
                ans = max - nowCnt;
            }
            if(map[nowY+1,nowX] != '#' && !passed[nowY+1,nowX])
            {
                yPos.Enqueue(nowY+1);
                xPos.Enqueue(nowX);
                cnt.Enqueue(nowCnt+1);
                passed[nowY+1,nowX] = true;
            }
            if(map[nowY-1,nowX] != '#' && !passed[nowY-1,nowX])
            {
                yPos.Enqueue(nowY-1);
                xPos.Enqueue(nowX);
                cnt.Enqueue(nowCnt+1);
                passed[nowY-1,nowX] = true;
            }
            if(map[nowY,nowX+1] != '#' && !passed[nowY,nowX+1])
            {
                yPos.Enqueue(nowY);
                xPos.Enqueue(nowX+1);
                cnt.Enqueue(nowCnt+1);
                passed[nowY,nowX+1] = true;
            }
            if(map[nowY,nowX-1] != '#' && !passed[nowY,nowX-1])
            {
                yPos.Enqueue(nowY);
                xPos.Enqueue(nowX-1);
                cnt.Enqueue(nowCnt+1);
                passed[nowY,nowX-1] = true;
            }
        }
    }

}

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

using namespace std;

typedef long long  ll;

const int MAX_WH = 1500 * 1000 + 5;
const int MAX_N = 1501;
int size_w[MAX_WH], size_h[MAX_WH];
int h[MAX_N], w[MAX_N];

int main()
{
    int n, m;

    while (true) {
        scanf("%d %d", &n, &m);
        if (n == 0 && m == 0)
            break;

        memset(size_h, 0, sizeof(size_h));
        memset(size_w, 0, sizeof(size_w));

        h[0] = w[0] = 0;
        for (int i = 0; i < n; ++i)
            scanf("%d", &h[i + 1]);
        for (int i = 0; i < m; ++i)
            scanf("%d", &w[i + 1]);

        ++n, ++m;
        for (int i = 1; i < n; ++i)
            h[i] += h[i - 1];
        for (int i = 1; i < m; ++i)
            w[i] += w[i - 1];

        for (int l = 0; l < n - 1; ++l)
            for (int r = l + 1; r < n; ++r)
                ++size_h[h[r] - h[l]];
        for (int l = 0; l < m - 1; ++l)
            for (int r = l + 1; r < m; ++r)
                ++size_w[w[r] - w[l]];

        int lim = min(h[n - 1], w[m - 1]);
        int ans = 0;
        for (int d = 1; d <= lim; ++d)
            ans += size_h[d] * size_w[d];

        cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 12936
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define set0(x) memset(x,0,sizeof(x))
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
template<typename T> void read(T &x){
	x = 0;char ch = getchar();int f = 1;
	while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
	while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
ll n,va,vb,a[5050],pos[5050],dp[5050][10010];
int main() {
	memset(dp,7,sizeof(dp));
	read(n);read(va);read(vb);
	for(int i=1;i<=n;i++){
		read(a[i]);
		pos[a[i]] = i;
	}
	dp[0][1] = 0;
	for(int i=1;i<=n;i++){
		ll cmin = 1e18;
		for(int j=1;j<=n*2+1;j++){
			if(j&1){
				cmin = min(cmin,dp[i-1][j]);
				cmin = min(cmin,dp[i-1][j-1]);
			}
			if(pos[i]*2 < j)dp[i][j] = cmin+va;
			if(pos[i]*2 == j)dp[i][j] = cmin;
			if(pos[i]*2 > j)dp[i][j] = cmin+vb;
		}
	}
	ll ans = 1e18;
	for(int j=1;j<=n*2+1;j++)ans = min(ans,dp[n][j]);
	cout<<ans<<endl;
	return 0;
}


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

int main(void) {
	int N, M, T[100], P[100], X[100], tmp, ans;
	cin >> N;
	for (int i = 0; i < N; i++) cin >> T[i];
	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> P[i] >> X[i];
	}
	for (int i = 0; i < M; i++) {
		tmp = T[P[i]-1];
		T[P[i]-1] = X[i];
		ans = 0;
		for (int j = 0; j < N; j++) {
			ans += T[j];
		}
		cout << ans << endl;
		T[P[i]-1] = tmp;
	}
}

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

int main() {
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	double d = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
	printf("%1f\n", d);

	return 0;
}

Measured memory_bytes: 3184
//#include <bits/stdc++.h>
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <iomanip>
#include <stack>
#include <bitset>

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define ROF(i, j, k) for(int i = (int)(j); i >= (int)(k); --i)
#define FORLL(i, n, m) for(long long i = n; i < (long long)(m); i++)
#define SORT(v, n) sort(v, v+n)
#define REVERSE(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;
const ll MOD=1000000007LL;
typedef pair<int, int> P;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }

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(a>b){a=b;return 1;}return 0;}

char A[10][10];

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

  int h, w, cnt = 0;
  cin >> h >> w;
  REP(i, h) REP(j, w) {
    cin >> A[i][j];
    if(A[i][j] == '#') cnt++;
  }
  if(cnt == w+h-1) cout << "Possible" << endl;
  else cout << "Impossible" << endl;
  
  return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] fib = new long[n+1];
        fib[0] = 1; fib[1] = 1;
        for (int i = 2; i <= n; i++) {
            fib[i] = fib[i-1] + fib[i-2];
        }
        System.out.println(fib[n]);
        sc.close();

    }

}



Measured memory_bytes: 26368
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <stack>

using namespace std;

vector<long long int> V;

long long int A[100005];

long long int x[100005], y[100005];

stack<long long int> L, R;

int main(void) {

	int N;
	cin >> N;
	for (int i = 1; i <= N; i++) {
		cin >> A[i];
		V.push_back(A[i]);
	}

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

	for (int i = 0; i < N; i++) {
		L.push(V[i]);
	}
	for (int i = N - 1; i >= 0; i--) {
		R.push(V[i]);
	}

	for (int i = 1; i <= N - 2; i++) {
		
		long long int a,b;
		
		a = L.top(); L.pop();
		b = R.top(); R.pop();

		long long int tmp1 = a - b;
		long long int tmp2 = b - a;

		long long int val1 = L.top();
		long long int val2 = R.top();

		long long int dis1 = 99999999999;
		dis1 = min(dis1, abs(tmp1 - val1));
		dis1 = min(dis1, abs(tmp1 - val2));

		long long int dis2 = 9999999999;
		dis2 = min(dis2, abs(tmp2 - val1));
		dis2 = min(dis2, abs(tmp2 - val2));

		if (dis1 >= dis2) {
			x[i] = a, y[i] = b; L.push(x[i] - y[i]);
		}
		else {
			x[i] = b; y[i] = a; R.push(x[i] - y[i]);
		}
	}


	x[N - 1] = max(L.top(),R.top());
	y[N - 1] = min(L.top(),R.top());

	cout << x[N - 1] - y[N - 1] << endl;
	for (int i = 1; i <= N - 1; i++) {
		cout << x[i] << " " << y[i] << endl;
	}

	return 0;
}

Measured memory_bytes: 6648
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;

const ll INF = 100000000LL;
const int MAX_N = 200005;

int h, w;
ll t[4 * MAX_N];
tuple<ll, int, int> tPr[4 * MAX_N];
ll tAdd[4 * MAX_N];

void build(int i, int l, int r)
{
	tPr[i] = { 0LL, 0, -1 };
	if (l != r)
	{
		int mid = (l + r) / 2;
		build(2 * i + 1, l, mid);
		build(2 * i + 2, mid + 1, r);
	}
}

void push(int i, int l, int r)
{
	if (l == r)
		return;

	int mid = (l + r) / 2;

	if (tAdd[i] != 0)
	{
		tAdd[2 * i + 1] += tAdd[i];
		tAdd[2 * i + 2] += tAdd[i];
		t[2 * i + 1] += tAdd[i];
		t[2 * i + 2] += tAdd[i];
		get<0>(tPr[2 * i + 1]) += tAdd[i];
		get<0>(tPr[2 * i + 2]) += tAdd[i];
		tAdd[i] = 0;
	}

	if (get<2>(tPr[i]) > get<2>(tPr[2 * i + 1]))
	{
		t[2 * i + 1] = get<0>(tPr[i]);
		tPr[2 * i + 1] = tPr[i];
	}

	if (get<2>(tPr[i]) > get<2>(tPr[2 * i + 2]))
	{
		t[2 * i + 2] = get<0>(tPr[i]) + ll(mid + 1 - l) * get<1>(tPr[i]);
		tPr[2 * i + 2] = { t[2 * i + 2], get<1>(tPr[i]), get<2>(tPr[i]) };
	}
}

void addOnRange(int i, int l, int r, int lx, int rx, int version)
{
	push(i, l, r);
	if (l == lx && r == rx)
	{
		t[i]++;
		get<0>(tPr[i])++;
		tAdd[i]++;
	}
	else
	{
		int mid = (l + r) / 2;
		if (lx <= mid)
			addOnRange(2 * i + 1, l, mid, lx, min(rx, mid), version);
		if (rx > mid)
			addOnRange(2 * i + 2, mid + 1, r, max(lx, mid + 1), rx, version);
		t[i] = min(t[2 * i + 1], t[2 * i + 2]);
	}
}

void setProgressionOnRange(int i, int l, int r, int lx, int rx, int startPos, ll startVal, int version)
{
	push(i, l, r);
	if (l == lx && r == rx)
	{
		t[i] = startVal + ll(l - startPos);
		tPr[i] = { t[i], 1, version };
	}
	else
	{
		int mid = (l + r) / 2;
		if (lx <= mid)
			setProgressionOnRange(2 * i + 1, l, mid, lx, min(rx, mid), startPos, startVal, version);
		if (rx > mid)
			setProgressionOnRange(2 * i + 2, mid + 1, r, max(lx, mid + 1), rx, startPos, startVal, version);
		t[i] = min(t[2 * i + 1], t[2 * i + 2]);
	}
}

ll getValue(int i, int l, int r, int pos)
{
	push(i, l, r);
	if (l == r)
		return t[i];
	else
	{
		int mid = (l + r) / 2;
		if (pos <= mid)
			return getValue(2 * i + 1, l, mid, pos);
		else
			return getValue(2 * i + 2, mid + 1, r, pos);
		t[i] = min(t[2 * i + 1], t[2 * i + 2]);
	}
}

int32_t main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> h >> w;
	build(0, 0, w - 1);
	for (int i = 0; i < h; i++)
	{
		int l, r;
		cin >> l >> r;
		l--; r--;

		if (l != 0)
			addOnRange(0, 0, w - 1, 0, l - 1, i);
		if (r != w - 1)
			addOnRange(0, 0, w - 1, r + 1, w - 1, i);

		ll startVal = (l == 0) ? INF : (getValue(0, 0, w - 1, l - 1) + 1LL);
		setProgressionOnRange(0, 0, w - 1, l, r, l, startVal, i);

		ll result = (t[0] >= INF) ? -1LL : t[0];
		cout << result << '\n';
	}

	return 0;
}

Measured memory_bytes: 20052