akhauriyash/Code-Regression huggingface dataset:
N=int(input())
A=list(map(int,input().split()))

ans1 = 0
cum1 = 0
for i in range(N):
    cum1 += A[i]
    if i % 2 == 0:
        if cum1 <= 0:
            ans1 += abs(cum1) + 1
            cum1 += abs(cum1) + 1
    else:
        if cum1 >= 0:
            ans1 += abs(cum1) + 1
            cum1 -= abs(cum1) + 1

ans2 = 0
cum2 = 0
for i in range(N):
    cum2 += A[i]
    if i % 2 == 1:
        if cum2 <= 0:
            ans2 += abs(cum2) + 1
            cum2 += abs(cum2) + 1
    else:
        if cum2 >= 0:
            ans2 += abs(cum2) + 1
            cum2 -= abs(cum2) + 1

print(min(ans1, ans2))

Measured memory_bytes: 84424
#include "bits/stdc++.h"
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define FILL(c, v) fill((c).begin(),(c).end(), v)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

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();}

typedef long long LL;
typedef unsigned long long ULL;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
int gcd(int a,int b){return b?gcd(b,a%b):a;}

void solve()
{
    int N,M; cin >> N >> M;
    int max_l = 0;
    int min_r = INTINF;
    REP0(i, M)
    {
        int l, r; cin >> l >> r;
        max_l = max(l, max_l);
        min_r = min(r, min_r);
    }
    cout << max(0, min_r - max_l + 1) << endl;
  
}

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(10);
    solve();
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
struct Fast {
    Fast() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} fast;
unsigned nChoosek(unsigned n, unsigned k) {
    if (k > n) return 0;
    if (k * 2 > n) k = n - k;
    if (k == 0) return 1;

    int result = n;
    for (int i = 2; i <= k; ++i) {
        result *= (n - i + 1);
        result /= i;
    }
    return result;
}

int main() {
    long long a, b;
    cin >> a >> b;
    cout << nChoosek(a, 2) + nChoosek(b, 2) << endl;
}


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

double get_radian(double x1, double y1, double x2, double y2) {
	if(fabs(x1-x2) < 0.00000001f) {
		if(fabs(y1-y2) < 0.00000001f) {
			return 0.0f;
		} else if(y1 > y2) {
			return 3 * M_PI / 2;
		} else {
			return M_PI / 2;
		}
	} else if(fabs(y1-y2) < 0.00000001f) {
		if(fabs(x1-x2) < 0.00000001f) {
			return 0.0f;
		} else if(x1 > x2) {
			return M_PI;
		}
		return 0.0;
	}
	
	if(x2 >= x1) {
		if(y2 >= y1) {
			return atan((y2-y1)/(x2-x1));
		}
		return 2 * M_PI + atan((y2-y1)/(x2-x1));
	} else {
		if(y2 >= y1) {
			return M_PI - atan((y2-y1)/(x1-x2));
		}
		return M_PI + atan((y2-y1)/(x2-x1));
	}
}

int is_crossing_line(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
	double S,T,D;

	//	分母を先に計算。計算量を減らすため、分子は後で行う。
	D = (x3-x1) * (y4-y1) - (y3-y1) * (x4-x1);
	
	//	D の符号に応じて分岐
	if(D < 0){
		S = (y4-y1)*(x2-x1) - (x4-x1)*(y2-y1);
		T = (x3-x1)*(y2-y1) - (y3-y1)*(x2-x1);
		
		return S <= 0 && T <= 0 && (S+T)/D >= 1;
	}
	else if(D > 0){
		S = (y4-y1)*(x2-x1) - (x4-x1)*(y2-y1);
		T = (x3-x1)*(y2-y1) - (y3-y1)*(x2-x1);
		
		return S >= 0 && T >= 0 && (S+T)/D >= 1;
	}
	
	//	D = 0 の時には、点が線分の上かどうかを判定する。
	return (x3-x1)*(x4-x1)+(y3-y1)*(y4-y1) <= 0
	    || (x3-x2)*(x4-x2)+(y3-y2)*(y4-y2) <= 0
	    || (x1-x3)*(x2-x3)+(y1-y3)*(y2-y3) <= 0;
}

int main(void) {
	double x1,x2,x3,x4,y1,y2,y3,y4;
	double ta, tb, tc, td;
	int i, n;
	
	scanf("%d\n", &n);
	for(i=0;i<n;i++) {
		scanf("%lf %lf %lf %lf %lf %lf %lf %lf\n", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);

		if(is_crossing_line(x1, y1, x2, y2, x3, y3, x4, y4)) {
			printf("1\n");
		} else {
			printf("0\n");
		}
	}
	
	return 0;
}

Measured memory_bytes: 620
n = int(input()) 
TXY = [list(map(int, input().split())) for _ in range(n)]
currT = 0
currX = 0
currY = 0
for t, x, y in TXY:
  nextT = t - currT
  nextStep = abs(x - currX) + abs(y - currY)
  if nextStep > nextT or (nextT - nextStep) % 2 == 1:
    print("No")
    exit()
  currT = t
  currX = x
  currY = y
print("Yes")

Measured memory_bytes: 27380
#include <iostream>

int main(int argc, char** argv)
{
    int N;
    std::cin >> N;

    int ans = 0;
    for (int i = N; i > 0; i--) {
        if (i < 10) {
            ans++;
        } else if (i < 100) {
        }
        else if (i < 1000) {
            ans++;
        }
        else if (i < 10000){
        }
        else if (i < 100000){
            ans++;
        } 
    }

    std::cout << ans << std::endl;

    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main()
{
    int a, b, n, result;
    
    while (scanf("%d %d", &a, &b) != EOF) {
        n = a + b;
        result = 0;
        
        while (n != 0) {
            n /= 10;
            result++;
        }
        
        printf("%d\n", result);
    }
    
    return 0;
}

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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver2 {
 public:
  LL n, m, v, p;
  vector<LL> a;
  Solver2(LL n, LL m, LL v, LL p) : n(n), m(m), v(v), p(p), a(n){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    sort(a.begin(), a.end());
    int lb = -1, ub = n - 1;
    while (ub - lb > 1) {
      int mid = (lb + ub) / 2;
      if (selected(mid)) {
        ub = mid;
      } else {
        lb = mid;
      }
    }
    cout << n - ub << endl;
  }
  bool selected(int x) {
    DBG(x, a)
    LL th = a[x] + m;
    LL rem = m * (v - 1);
    int pos = n - 1;
    int cnt = 0;
    DBG(pos, th, rem)
    while (pos >= 0 && cnt < p - 1) {
      if (pos == x) {
        pos--;
        continue;
      }
      rem -= m;
      if (a[pos] + m > th) cnt++;
      pos--;
    }
    DBG(pos, rem, cnt)

    for (; pos >= 0; --pos) {
      if (pos == x) {
        continue;
      }
      if (a[pos] > th) return false;
      rem -= min(m, th - a[pos]);
    }
    DBG(rem)
    return rem <= 0;
  }
};
}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0, m, v, p;
  std::cin >> n >> m >> v >> p;

  Problem::Solver2 sol(n, m, v, p);
  sol.solve();
  return 0;
}


Measured memory_bytes: 1024
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===
def main():
    x = ni()

    base = x // 11 * 2
    mod = x % 11
    if mod == 0:
        hoge = 0
    elif mod > 6:
        base += 2
    else:
        base += 1

    print(base)


if __name__ == '__main__':
    main()


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

const int mxN=2e3, M=1e9+7;
int n, m, k, dp[mxN+1][mxN+1];

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

	cin >> n >> m >> k;
	for(int i=1; i<k; ++i)
		dp[i][k-i]=1;
	for(int a=2*k-1; a<=n+m; a+=k-1) {
		for(int i=max(a-m, 1); i<=min(a-1, n); ++i) {
			int j=a-i;
			dp[i][j]=1;
			for(int l=max(k-j, 0); l<min(k, i); ++l)
				dp[i][j]=(dp[i][j]+dp[i-l][j-(k-1-l)])%M;
		}
	}
	cout << dp[n][m];
}

Measured memory_bytes: 15872
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>

using namespace std;

int W,H;
char s;


int main(){
    while(cin>>H>>W){
        if(W==0&&H==0)break;
        
        int tile[2][101][101]={0};
        bool pass[101][101]={0};
        
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                cin>>s;
                if(s=='>'){
                    tile[0][i][j]=0;
                    tile[1][i][j]=1;
                }else if(s=='<'){
                    tile[0][i][j]=0;
                    tile[1][i][j]=-1;
                }else if(s=='^'){
                    tile[0][i][j]=-1;
                    tile[1][i][j]=0;
                }else if(s=='v'){
                    tile[0][i][j]=1;
                    tile[1][i][j]=0;
                }else if(s=='.'){
                    tile[0][i][j]=0;
                    tile[1][i][j]=0;
                }
            }
        }
        
        int now_w=0,now_h=0;
        while(1){
            //cout<<now_w<<" "<<now_h<<endl;
            
            if(tile[0][now_h][now_w]==0&&tile[1][now_h][now_w]==0)break;
            if(pass[now_h][now_w]==true){
                now_w=-1;
                now_h=-1;
                break;
            }
            
            pass[now_h][now_w]=true;
            int temp_h,temp_w;
            temp_h=now_h+tile[0][now_h][now_w];
            temp_w=now_w+tile[1][now_h][now_w];
            now_h=temp_h;
            now_w=temp_w;
            
        }
        
        if(now_h==-1){
            cout<<"LOOP"<<endl;
        }else{
            cout<<now_w<<" "<<now_h<<endl;
        }
        
        
        
        
    }
    
}

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

typedef pair <int , int> PII;

priority_queue <PII> q;
int n,m;
int t;

int main()
{
	cin>>n>>m;
	
	long long s=0;
	for(int i=0; i<n; i++)
	{
		cin>>t;
//		s+=x;
		q.push(make_pair(t/2,t));
	}
	
	for(int i=0; i<m; i++)
	{
		PII a=q.top();
		
		if(i!=m-1)
		{
			q.pop();
			q.push(make_pair(a.first/2,a.first));
		}
	}
	
	s+=q.top().first;
	q.pop();
	for(int i=1; i<n; i++)
	{
		s+=q.top().second;
		q.pop();
	}
	
	cout<<s<<endl;
	
	return 0;
}

Measured memory_bytes: 1400
#include <iostream>

using namespace std;

int main(void)
{
    int str_length;
    string str;

    cin >> str_length;
    cin >> str;

    int count = 0;
    int i = 0;
    while(i < str_length){
        if(str[i] == 'A'){
            if(i + 1 < str_length && str[i + 1] == 'B'){
                if(i + 2 < str_length && str[i + 2] == 'C'){
                    count += 1;
                }
                i += 2;
            }else{
                i += 1;
            }
        }else{
            i += 1;
        }
    }

    cout << count;

    return 0;
}

Measured memory_bytes: 256
declare -r R4=(0 7 5 3 1 8 6 4 2) 

solve() {
    z1=$((Y / 1000))
    (( z1 < N )) && return 1
    y=${R4[(z1 - N) % 9]}
    x=$(( (z1 - N - 4 * y) / 9 ))
    z=$(( N - x - y ))
    (( x >= 0 && y >= 0 && z >= 0 )) || return 1
    echo $x $y $z
}

read N Y
solve || echo -1 -1 -1


Measured memory_bytes: 376
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 11000
#define LL long long
#define ll __int64
#define INF 0x3f3f3f3f
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
#define RD(n) scanf("%d",&n)
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head  ios::sync_with_stdio(false);


/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least three) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
    7.use scanf instead of cin/cout?
    8.whatch out the detail input require
*/
int a[10005],b[10005];
int main()
{
	int n;
	scanf("%d",&n);
	LL _=0,__=0;
	for(int i=1;i<=n;i++)
		RD(a[i]);
	for(int i=1;i<=n;i++)
	{
		RD(b[i]);
		if(a[i]<b[i])
		{	
			
			if((b[i]-a[i])%2!=0)
			{
				__+=1LL;_+=1LL*(b[i]-a[i]+1)/2;
			}
			else
			{
				_+=1LL*(b[i]-a[i])/2;
			}
				
		}
		else
		{
			__+=1LL*(a[i]-b[i]);
		}
	}
	if(_>=__)puts("Yes");
	else
		puts("No");
}



Measured memory_bytes: 384
#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline 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);
  int n; cin >> n;
  vector<vi> d(n,vi(2));
  rep(i,n) cin >> d[i][0] >> d[i][1];
  int cnt = 0;
  rep(i,n){
    if(d[i][0] == d[i][1]) cnt++;
    else cnt = 0;
    if(3 <= cnt){
      cout << "Yes";
      return 0;
    }
  }
  cout << "No";
  return 0;
}


Measured memory_bytes: 3664
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

const int MAX = 510000;
const int MOD = 1000000007;
const int MAX_V = 200;

int d[MAX_V][MAX_V];
int V;

void warshall_floyd() {
    for(int k = 0; k < V; k++) {
        for(int i = 0; i < V; i++) {
            for(int j = 0; j < V; j++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

int main(){
  ll H ,W; cin >> H >> W;
  V = 10;
  rep(i,V){
    rep(j,V){
      ll a; cin >> a;
      d[i][j] = a;
    }
  }
  warshall_floyd();
  ll ans = 0;
  rep(i,H){
    rep(j,W){
      ll b; cin >> b;
      if(b == -1) continue;
      else{
        ans += d[b][1];
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(){
	int n,l,ans;
	scanf("%d%d",&n,&l);
    int a,b;/*aは初項bは末項*/
	a=l;
	b=n+l-1;
	if(a<=0&&b>=0){
		ans =n*(a+b)/2;
	}
	else if(a<0&&b<0){
		ans = n*(a+b)/2-b;
	}
	else{
		ans =n*(a+b)/2-a;
	}
	printf("%d",ans);
	return 0;
}

		

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

int main() {
  char c;
  cin >> c;
  
  if (c == 'a' || c == 'e' || c=='i' || c=='o'||c=='u'){
    cout << "vowel" << endl;
  }
  else {
    cout << "consonant" << endl;
  }
}


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

#define fi first
#define se second
#define show(x) cerr << #x << "=" << "\n"
typedef long long ll;
typedef unsigned long long ull;

using namespace std;

int n, m, s, t;
vector<pair<int, ll> > yen[100001], snuke[100001];
ll d[100001], d2[100001], ans[100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  FILE *stream1;
  //FILE *stream2;
  stream1 = freopen("in.txt", "r", stdin);
  //stream2 = freopen("out.txt", "w", stdout);
  if (stream1 == NULL) return 0;
  //if (stream2 == NULL) return 0;
#endif
  cin >> n >> m >> s >> t;
  s--; t--;
  for (int i = 0; i < m; i++) {
    int v, u; cin >> v >> u;
    v--; u--;
    ll cost1, cost2;
    cin >> cost1 >> cost2;
    yen[v].push_back({u, cost1});
    yen[u].push_back({v, cost1});
    snuke[v].push_back({u, cost2});
    snuke[u].push_back({v, cost2});
  }
  for (int i = 0; i < n; i++) d[i] = LLONG_MAX;
  d[s] = 0;
  priority_queue<pair<ll, int > > q;
  q.push({0, s});
  while (!q.empty()) {
    int nowv = q.top().se;
    ll nowcost = -q.top().fi;
    q.pop();
    if (nowcost > d[nowv]) continue;
    for (int i = 0; i < (int)yen[nowv].size(); i++) {
      int nv = yen[nowv][i].fi;
      ll ncost = yen[nowv][i].se + nowcost;
      if (d[nv] > ncost) {
        q.push({-ncost, nv});
        d[nv] = ncost;
      }
    }
  }
  for (int i = 0; i < n; i++) d2[i] = LLONG_MAX;
  d2[t] = 0;
  priority_queue<pair<ll, int> > q2;
  q2.push({0, t});
  while (!q2.empty()) {
    int nowv = q2.top().se;
    ll nowcost = -q2.top().fi;
    q2.pop();
    if (nowcost > d2[nowv]) continue;
    for (int i = 0; i < (int)snuke[nowv].size(); i++) {
      int nv = snuke[nowv][i].fi;
      ll ncost = snuke[nowv][i].se + nowcost;
      if (d2[nv] > ncost) {
        q2.push({-ncost, nv});
        d2[nv] = ncost;
      }
    }
  }
  ll inf = 1000000000000000;
  for (int i = n - 1; i >= 0; i--) {
    ans[i] = max(ans[i + 1], inf - d[i] - d2[i]);
  }
  // for (int i = 0; i < n; i++) {
  //   cerr << "d" << i << ": " << d[i] << endl;
  // }
  // for (int i = 0; i < n; i++) {
  //   cerr << "d2" << i << ": " << d2[i] << endl;
  // }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n";
  fclose(stream1);
  //fclose(stream2);
#endif
  return 0;
}

Measured memory_bytes: 20216
#include <stdio.h>
int main()
{
	int a,b,x;
	char op;	

	while(1){
		scanf("%d %c %d",&a,&op,&b);
		if(op=='+'){
			x=a+b;
		}else if(op=='-'){
			x=a-b;
		}else if(op=='*'){
			x=a*b;
		}else if(op=='/'){
			x=a/b;
		}else{
			break;
		}
		printf("%d\n",x);
	}
	return 0;
}


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

public class Main {
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
        int h = sc.nextInt(); int w = sc.nextInt(); int k = sc.nextInt();
        int [][] a = new int[h][w];
        for (int i = 0; i < h; i++) {
            String s = sc.next();
            for (int j = 0; j < w; j++) {
                a[i][j] = (s.charAt(j) == '.' ? 0 : 1);
            }
        }
        int ans = 0;
        for (int row = 0; row < (1 << h); row++) {
            for (int col = 0; col < (1 << w); col++) {
                int [][] copy = new int[h][w];
                for (int i = 0; i < h; i++) {
                    for (int j = 0; j < w; j++) {
                        copy[i][j] = a[i][j];
                    }
                }
                for (int b = 0; b < h; b++) {
                    if (((row >> b) & 1) == 1) {
                        for (int j = 0; j < w; j++) copy[b][j] = -1;
                    }
                }
                for (int b = 0; b < w; b++) {
                    if (((col >> b) & 1) == 1) {
                        for (int i = 0; i < h; i++) copy[i][b] = -1;
                    }
                }
                int ct = 0;
                for (int i = 0; i < h; i++) {
                    for (int j = 0; j < w; j++) {
                        if (copy[i][j] == 1) ct++;
                    }
                }
                if (ct == k) ans++;
            }
        }
        out.println(ans);
        out.close();
    }


    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }


    }

}

Measured memory_bytes: 36384
#include<stdio.h>
int main()
{
    int x,y,z1,z2;
    scanf("%d ",&x);
    scanf("%d\n",&y);
    z1=x*y;
    z2=2*(x+y);
    printf("%d %d\n",z1,z2);
    
    return 0;
}


Measured memory_bytes: 2044
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }

constexpr int INF = 1e9+5;
constexpr long long INFLL = 1LL<<60;
constexpr double eps = (1e-9);

// 2部グラフ判定
// 頂点 v を c で塗る. v に隣接するノードは -c
bool dfs(const vector<vector<int>> g, vector<int>& color, int v, int c) {
  color[v] = c;
  for (int i = 0; i < g[v].size(); i++) {
    if (color[g[v][i]] == c) return false;   // 隣接するノードが同じ色で塗られていた
    if (color[g[v][i]] == 0 && !dfs(g, color, g[v][i], -c)) return false;  // 隣接するノードを -c で塗れない
  }
  return true;
}

void warshall_floyd(const int& n, vector<vector<int>>& d) {
  rep(k, n) rep(i, n) rep(j, n) chmin(d[i][j], d[i][k] + d[k][j]);
}

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

  int n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  vector<vector<int>> g(n, vector<int>());   // グラフの辺
  rep(i, n) rep(j, n) {
    if (s[i][j] == '1') g[i].push_back(j);
  }

  vector<int> color(n, 0);

  if (!dfs(g, color, 0, 1)) {
    cout << -1 << endl;
    return 0;
  }

  vector<vector<int>> d(n, vector<int>(n, INF));
  rep(i, n) rep(j, n) {
    if (i == j) d[i][j] = 0;
    else if (s[i][j] == '1') d[i][j] = 1;
  }

  warshall_floyd(n, d);

  int dia = 0;
  for (auto& v : d) {
    chmax(dia, MAX(v));
  }

  cout << dia + 1 << endl;

  return 0;
}


Measured memory_bytes: 20732
#include <cstring>
#include <vector>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <cstring>
#include <map>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(int i=0;i<(n);i++)
#define mp(a,b) make_pair((a),(b))

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

const int INF=100000000;
const long long int INF_=1000000000000000000;

int N,M,H,K; //縦棒の本数、横棒の本数、縦棒の長さ、選ぶ縦棒の本数

bool isIn(int x){
	return 0<=x&&x<K;
}

int pre(const vector<int> &s,vector<int> &s_,const vector<vector<int> > &r,vector<vector<P> > &b){
	int result=0;
	rep(i,K){
		int x=i;
		rep(j,H){
			if(r[x][j]==1){
				b[x][j].first=i;
				++x;
			} else if(x!=0&&r[x-1][j]==1){
				b[x-1][j].second=i;
				 --x;
			}
		}
		s_[i]=s[x];
		result+=s[x];
	}
	return result;
}

int solve(const vector<int> &s,const vector<int> &s_,vector<vector<int> > &vvi,const vector<vector<P> > &b,const int X,const int Y){
	int result=0;
	result-=(isIn(b[X][Y].first)?s_[b[X][Y].first]:0)+(isIn(b[X][Y].second)?s_[b[X][Y].second]:0);
	vvi[X][Y]=0;
	if(isIn(b[X][Y].first)){
		int x=b[X][Y].first;
		rep(i,H){
			if(vvi[x][i]==1) ++x;
			else if(x!=0&&vvi[x-1][i]==1) --x;
		}
		result+=s[x];
	}
	if(isIn(b[X][Y].second)){
		int x=b[X][Y].second;
		rep(i,H){
			if(vvi[x][i]==1) ++x;
			else if(x!=0&&vvi[x-1][i]==1) --x;
		}
		result+=s[x];
	}
	vvi[X][Y]=1;
	return result;
}

int main(){
	while(cin >> N >> M >> H >> K){
		if(N+M+H+K==0) break;
		vector<int> score(N);
		vector<int> score_(N);
		vector<vector<int> > rail(N,vector<int>(H)); //何番の棒を結ぶか、上端からの距離、横棒が存在するなら1
		vector<P> rail_(M); //横棒のx,yを保存
		rep(i,N) cin >> score[i];
		rep(i,M){
			int a,b;
			cin >> a >> b;
			--a;
			rail_[i].first=a;
			rail_[i].second=b;
			rail[a][b]=1;
		}
		vector<vector<P> > bar(N,vector<P>(H,P(-1,-1))); //x,yにある横棒とつながっているスタート地点の番号
		int ans,sum;
		ans=sum=pre(score,score_,rail,bar);
		rep(i,M){
			ans=min(ans,sum+solve(score,score_,rail,bar,rail_[i].first,rail_[i].second));
		}
		cout << ans << endl;
	}
	return 0;
}

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

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; }

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1<<30;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
    ll N;
    cin>>N;
    vector<ll> v(N);
    rep(i,N) cin>>v[i];
    vector<ll> w(N/2);
    for(ll i = 0; i < N; i+=2){
        w[i/2]=v[i]^v[i+1];
    }
    ll ans = 0;
    for(auto x:w){
        ans=(ans^x);
    }
    for(auto x:v){
        cout<<(ans^x)<<" ";
    }
  	cout<<endl;
}

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

using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
typedef long long ll;
typedef pair<int, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = LONG_LONG_MAX;
//const int INF = INT_MAX  ;
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)

const int mod = 1000000007;

int dp[101][4][4][4];

int main() {
    int H, W, h, w;
    cin >> H >> W >> h >> w;

    int ans = H * W -  (h * W + H * w - h * w);

    cout << ans << endl;

}



Measured memory_bytes: 256
from itertools import permutations

def wfa(N):
    for i in range(N + 1):
        for j in range(N + 1):
            w_ji = way[j][i]
            for k, (w_ik, w_jk) in enumerate(zip(way[i], way[j])):
                if k == 0:
                    continue
                tmp = w_ji + w_ik
                if w_jk > tmp:
                    way[j][k] = tmp


N, M, R = map(int, input().split())
r = list(map(int, input().split()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

wfa(N)

ans = float("inf")
for path in permutations(r):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
print(ans)


Measured memory_bytes: 5616
def main():
    N, W = [int(n) for n in input().split()]
    
    baggages = [[int(n) for n in input().split()] for i in range(N)]# 荷物1, 荷物2, ...と数え、荷物0は数合わせの(0, 0)
    baggages.insert(0, [0, 0])

    VALUE, WEIGHT = 1, 0
    VALUE_LIMIT = int(1e5)
    INF = int(1e15)

    
    dp = [[-1] * (VALUE_LIMIT+1) for n in range(N+1)]# 荷物は0~N, 価値は0~VALUE_LIMIT
    # dp[baggage][value] := baggage番目までの荷物を使ってvalueだけとる最小重量
    dp[0] = [INF] * (VALUE_LIMIT+1)
    dp[0][0] = 0
    
    for baggage in range(1, N+1):
        for value in range(VALUE_LIMIT+1):
            if dp[baggage][value] != -1:
                continue
            bag_w = baggages[baggage][WEIGHT]
            bag_v = baggages[baggage][VALUE]
            val1 = dp[baggage-1][value]
            val2 = (dp[baggage-1][value-bag_v] + bag_w) if value >= bag_v else INF
            dp[baggage][value] = min(val1, val2)
    
    max_val = 0
    for index, w in enumerate(dp[N]):
        if w <= W:
            max_val = index
    print(max_val)


if __name__ == "__main__":
    main()

Measured memory_bytes: 118000
# Date [ 2020-09-13 21:28:48 ]
# Problem [ d.py ]
# Author Koki_tkg

import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

class Combination:
    def __init__(self, n, p):
        self.n = n
        self.p = p
        self.note = [1, 1]
        self.noteinv = [1, 1]
        self.inv = [0, 1]
        self.calc()

    def calc(self):
        for i in range(2, self.n + 1):
            self.note.append((self.note[-1] * i) % self.p)
            self.inv.append((-self.inv[self.p % i] * (self.p // i)) % self.p)
            self.noteinv.append((self.noteinv[-1] * self.inv[-1]) % self.p)
    
    def nCr(self, n, r):
        if r < 0 or n < r:
            return 0
        r = min(r, n - r)
        return (self.note[n] * self.noteinv[r] * self.noteinv[n - r]) % self.p

def mypow(self, x: int, n: int) -> int:
    r = 1; m = MOD
    while n:
        if n & 1: r = r * x % m
        x = x * x % m
        n >>= 1
    return r

def Main():
    s = read_int()
    num = s // 3
    amari = s % 3
    if s < 3:
        print(0)
        exit()
    if s <= 5:
        print(1)
        exit()
    if amari == 0:
        ans = 1
    elif amari == 1:
        ans = num
    elif amari == 2:
        ans = num * (num - 1) // 2 + num
    cmb = Combination(s + 1, MOD)
    amari += 3
    num -= 1
    while num>1:
        ans += cmb.nCr(amari + num - 1, num-1)
        ans %= MOD
        amari += 3
        num -= 1
    ans = (ans + 1) % MOD
    print(ans)

if __name__ == '__main__':
    Main()

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

public class Main {
	static PrintWriter out;
	static StringBuilder sb;
	static int mod = 998244353;
	static int inf = (int) 1e15;
	static ArrayList<Integer>[] ad;
	static int n, s;
	static int[][][] memo;
	static int[] a;

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		out = new PrintWriter(System.out);
	    long n=sc.nextLong();
	    long k=sc.nextLong();
	    System.out.println(Math.min(n%k, (k-n%k)%k));
		out.close();
	}

	static class pair {
		int id;
		int v;

		public pair(int i, int vi) {
			id = i;
			v = vi;
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public int[] nextArrInt(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public long[] nextArrLong(int n) throws IOException {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


Measured memory_bytes: 22868
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <tuple>
#include <map>
#include <unordered_map>
#include <deque>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[])
{
	int q, i;
	deque<char> s;
	string tmp;
	int ope, option;
	bool rev = false;
	char addchar;
	cin >> tmp;
	cin >> q;

	for (auto it = tmp.begin(); it != tmp.end(); it++)
		s.push_back(*it);
	
	for (i = 0; i < q; i++) {
		cin >> ope;
		if (ope == 1) {
			rev = !rev;
		} else if (ope == 2) {
			cin >> option >> addchar;
			if (rev) 
				option = option % 2 + 1;
			if (option == 2)
			    s.push_back(addchar);
			else
				s.push_front(addchar);
		}
	}
	if (rev)
		reverse(s.begin(), s.end());

	for (auto it = s.begin(); it != s.end(); it++)
		cout << *it;
	cout << endl;
	return 0;
}


Measured memory_bytes: 896
#include "bits/stdc++.h"
using namespace std;
#define PIGRECO 3.141592653589793

#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 {
	~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 << "]";
}
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////

const int MAXN = 13;
const int MAXM = 13;


int vals[MAXN][MAXM];
int c[MAXN];

int n, m, x;

int ans;

void get_plus(int sums1[], int sums2[]) {




	for (int i = 1; i <= m; i++) {
		sums1[i] += sums2[i];
	}





}


void get_minus(int sums1[], int sums2[]) {




	for (int i = 1; i <= m; i++) {
		sums1[i] -= sums2[i];
	}





}

bool finished(int sums[]) {


	for (int i = 1; i <= m; i++) {
		if (sums[i] < x)return false;
	}


	return true;


}


void search(int cost, int sums[], int pos) {


	if (finished(sums)) {

		ans = min(ans, cost);
		return;

	}


	// cout << cost << endl;

	// for (int i = 1; i <= m; i++) {
	// 	cout << sums[i] << " ";
	// }

	// cout << endl;





	for (int i = pos; i <= n; i++) {

		get_plus(sums, vals[i]);

		search(cost + c[i], sums, i + 1) ;

		get_minus(sums, vals[i]);


	}


}




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


	cin >> n >> m >> x;


	ans = INT_MAX;




	for (int i = 1; i <= n; i++) {
		cin >> c[i];
		for (int j = 1; j <= m; j++) {
			cin >> vals[i][j];
		}
	}



	int start[m + 1];

	memset(start, 0, sizeof(start));

	search(0, start, 1);

	if (ans == INT_MAX) {
		cout << -1 << endl;
		return 0;
	}


	cout << ans << endl;


}





Measured memory_bytes: 3652
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << endl;
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

void greedy(int N, const vll& A) {
    ll cur = 1000;
    REP(i, N - 1) {
        ll stock = 0;
        if (A[i] < A[i + 1]) stock = cur / A[i];
        cur += (A[i+1] - A[i]) * stock;
    }

    print(cur);
}

void chmax(ll& a, ll b) {
    if (a < b) a = b;
}


void dp(int N, const vll& A) {
    vll dp(N + 1, 0);
    dp[0] = 1000;
    REP(i, N) {
        REP(j, i + 1) {
            chmax(dp[i+1], dp[j] % A[j] + dp[j] / A[j] * A[i]);
        }
    }

    print(dp[N]);
}


int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    
    int N;
    cin >> N;

    vll A(N);
    REP(i, N) cin >> A[i];

//    greedy(N, A);

    dp(N, A);
}


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

int n,m,x,A[20][20];
int main(){
    cin>>n>>m>>x;
    for(int i=0;i<n;++i){
        for(int j=0;j<m+1;++j){
            cin>>A[i][j];
        }
    }
    long long  ans=10000000000;
    for(int msk=0;msk<(1<<n);++msk){
        int skill[20]={0};
        long long cur=0;
        for(int i=0;i<n;++i){
            if(msk&(1<<i)){
                cur+=A[i][0];
                for(int j=1;j<m+1;++j){
                    skill[j-1]+=A[i][j];
                }
            }
        }
        int cnt=0;
        for(int i=0;i<m;++i){
            if(skill[i]>=x){
                ++cnt;
            }
        }
        if(cnt==m){
            ans=min(ans,cur);
        }
    }
    if(ans==10000000000) cout<<-1<<"\n";
    else cout<<ans<<"\n";
}


Measured memory_bytes: 3600
A,B,C,X,Y = map(int,input().split(" "))
list = []
list.append(A*X+B*Y)
less = min(X,Y)
if X > Y:
    list.append(2*C*less + A * (X - less))
else:
    list.append(2*C*less + B * (Y - less))
more = max(X,Y)
if X > Y:
    list.append(2*C*more)
else:
    list.append(2*C*more)
print(min(list))

Measured memory_bytes: 3064
#include <cstdio>

#define MAX 200000

int main()
{
	int N, ans;
	char st[MAX];
	scanf("%d", &N);
	scanf("%s", st);
	ans = N;
	for(int i=0; i<N-1; i++){
		if(st[i] == st[i+1]){
			ans--;
		}
	}
	printf("%d\n", ans);
	return 0;
}

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

using namespace std;

int main() {
	string s;
	int x, y;
	bool ok1 = 0;
	bool ok2 = 0;
	
	cin >> s;
	x = (s[1] - '0') + (s[0] - '0') * 10;
	y = (s[3] - '0') + (s[2] - '0') * 10;
	
	if (x >= 1 && x <= 12){
		ok1 = 1;
	}
	
	if (y >= 1 && y <= 12){
		ok2 = 1;
	}
	if (ok1 && ok2)
		cout << "AMBIGUOUS";
	else if (ok1)
		cout << "MMYY";
	else if (ok2)
		cout << "YYMM";
	else
		cout << "NA";

	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	int group_num,value;
};

int H,W,SUPPLY,SUM;
int demand[32][32];
Info dp[32][32][32][32];

int calc_demand_sum(int row1,int col1,int row2,int col2){
	int A = 0,B = 0,C = 0;

	if(row1 == 0 || col1 == 0){
		C = 0;
	}else{
		C = demand[row1-1][col1-1];
	}

	if(row1 == 0){
		B = 0;
	}else{
		B = demand[row1-1][col2];
	}

	if(col1 == 0){
		A = 0;
	}else{
		A = demand[row2][col1-1];
	}

	return demand[row2][col2]-A-B+C;
}

Info recursive(int row1,int col1,int row2,int col2){

	 if(dp[row1][col1][row2][col2].group_num != -1)return dp[row1][col1][row2][col2];

	    Info ret;
	    ret.group_num = -BIG_NUM;
	    ret.value = BIG_NUM;

	    int rest = SUM - calc_demand_sum(row1,col1,row2,col2);
	    if(rest <= SUPPLY) {
	        ret.group_num = 1;
	        ret.value = -rest;

	        for(int bottom_row = row1; bottom_row < row2; bottom_row++){

				Info ret_a = recursive(row1,col1,bottom_row,col2);
				if(ret_a.group_num == -BIG_NUM)continue;
				Info ret_b = recursive(bottom_row+1,col1,row2,col2);
				if(ret_b.group_num == -BIG_NUM)continue;

				Info ret_c;
				ret_c.group_num = ret_a.group_num+ret_b.group_num;
				ret_c.value = min(ret_a.value,ret_b.value);

				if((ret.group_num < ret_c.group_num) || (ret.group_num == ret_c.group_num && ret.value < ret_c.value)){
					ret = ret_c;
				}
			}

			for(int mid_col = col1; mid_col < col2; mid_col++){

				Info ret_a = recursive(row1,col1,row2,mid_col);
				if(ret_a.group_num == -BIG_NUM)continue;
				Info ret_b = recursive(row1,mid_col+1,row2,col2);
				if(ret_b.group_num == -BIG_NUM)continue;

				Info ret_c;
				ret_c.group_num = ret_a.group_num+ret_b.group_num;
				ret_c.value = min(ret_a.value,ret_b.value);

				if((ret.group_num < ret_c.group_num) || (ret.group_num == ret_c.group_num && ret.value < ret_c.value)){
					ret = ret_c;
				}
			}
	    }
	    return dp[row1][col1][row2][col2] = ret;
}

void func(){

	SUM = 0;
	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){
			scanf("%d",&demand[row][col]);
			SUM += demand[row][col];
		}
	}

	for(int row = 0; row < H; row++){
		for(int col = 1; col < W; col++){
			demand[row][col] += demand[row][col-1];
		}
	}

	for(int col = 0; col < W; col++){
		for(int row = 1; row < H; row++){
			demand[row][col] += demand[row-1][col];
		}
	}

	for(int a = 0; a < H; a++){
		for(int b = 0; b < W; b++){
			for(int c = 0; c < H; c++){
				for(int d = 0; d < W; d++){

					dp[a][b][c][d].group_num = -1;
					dp[a][b][c][d].value = -1;
				}
			}
		}
	}

	Info ans = recursive(0,0,H-1,W-1);

	printf("%d %d\n",ans.group_num,SUPPLY+ans.value);
}

int main(){

	while(true){
		scanf("%d %d %d",&H,&W,&SUPPLY);
		if(H == 0 && W == 0 && SUPPLY == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 11348
#include <iostream>
#include <cstring>
#include <stack>

using namespace std;
 
struct set { int next, sum, depth; };
 
int n, s, cnt;

void dfs(int next, int sum, int depth) {
    stack<set> S;
    set tmp;
    tmp.next = next; tmp.sum = sum; tmp.depth = depth;
    S.push(tmp);
    while (!S.empty()) {
        set u = S.top();
        // cout << u.next << " " << u.sum << " " << u.depth  << endl;
        if (u.depth == n && u.sum == s) {
            cnt++;
        }
        if (u.next == 10 && u.depth == 0 && u.sum == 0) break;

        if (u.next < 10 && u.depth < n) {
            tmp.next = u.next + 1;
            tmp.sum = u.sum;
            tmp.depth = u.depth;
            S.push(tmp);
 
            tmp.next = u.next + 1;
            tmp.sum = u.sum + u.next;
            tmp.depth = u.depth + 1;
            S.push(tmp);
        }
        else if (u.next == 10) {
            int tmp = u.depth;
            while (S.top().depth == tmp) {
                S.pop();
            }
        }
        else {
            S.pop();
        }
    }
}
 
int main() {
    while (cin >> n >> s) {
        if (n == 0 && s == 0) break;
        cnt = 0;
        dfs(0, 0, 0);
        cout << cnt << endl;
    }
    return 0;
}

Measured memory_bytes: 2996
#include <iostream>
#include <iomanip>
#include <map>
#include <utility>
typedef long long ll;
using namespace std;
const ll INF = 1e9;
const ll MOD = 998244353;
#define repi(i, n, init) for (ll i = init; i < (n); i++)

ll power(ll n, ll t)
{
    ll r = 1;
    repi(i, t, 0)
    {
        r *= n;
        r %= MOD;
    }
    return r % MOD;
}
int main()
{
    int n;
    cin >> n;
    map<int, int> rt;
    repi(i, n, 0)
    {
        int d;
        cin >> d;
        if(i == 0 && d != 0){
            cout << 0 << endl;
            return 0;
        }
        rt[d]++;
    }
    if (rt[0] != 1)
    {
        cout << 0 << endl;
    }
    else
    {
        ll ans = 1;
        repi(i, rt.size(), 1)
        {
            ans *= power(rt[i - 1], rt[i]);
            ans %= MOD;
        }
        cout << ans % MOD << endl;
    }
    return 0;
}

Measured memory_bytes: 4992
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll n;
  cin >> n;
  vec a(n);
  ll x=0;
  for(ll i=0;i<n;i++) {
    cin >> a[i];
    if(i>1) x^=a[i];
  }
  ll s=a[0]+a[1];
  ll d=(s-x)/2;
  if(s-x<0||(s-x)%2||d&x||d>a[0]) {
    cout << -1 << endl;
    return 0;
  }
  ll ans=d;
  for(ll i=(1LL<<61);i;i>>=1) {
    if(!(i&x)) continue;
    if(ans+i<a[0]) ans+=i;
  }
  if(ans) cout << a[0]-ans << endl;
  else cout << -1 << endl;
}

Measured memory_bytes: 3604
#include<iostream>

using namespace std;
int main(){

	double N=0;
	double count = 0;
	cin >> N;

	for (int i = 1; i < N+1; i++)
	{
		if (i% 2 != 0)
		{
			count++;
		}
	}
	count=count / N;

	cout << count;
	return 0;
}

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

typedef long double ld;									// XD
#define int long long
#define PB push_back
#define all(x) (x).begin(),(x).end()
#define ii pair <int,int>
#define mii map <int,int>
#define vi vector<int>
#define vii vector< ii >
#define F first
#define S second
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a; i>=b; --i)
#define sz(x) (int)((x).size())
#define mem(a,x) memset(a,x,sizeof(a))
const long double PI=3.141592653589793238462643383279502884197169399375105820974944;
inline void io(){
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

const int mod = 1e9 + 7;
const int inf = 3e16;
const int maxn = 1e5+5;

void solve(){
	int ans = inf;
	string s, t; cin>>s>>t;
	int n = sz(s), m = sz(t);
	rep(i, 0, n-m+1){
		string k = s.substr(i, m);
		int res = 0;
		rep(j, 0, m){
			if(t[j] != k[j]) res++;
		}
		ans = min(ans, res);
	}
	cout<<ans;
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0); //io();
    //cout<<setprecision(12);
    int tc=1;// cin>>tc;
    while(tc--){
        solve();cout<<'\n';
    }
    return 0;
}


Measured memory_bytes: 3660
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <string>
#include <math.h>
#include <cstdlib>
#include <istream>
#include <sstream>
#include <cctype>
#include <iomanip>
//cout << fixed << setprecision(20) << *** << endl;
#define rep(i,N) for(int i=0;i<(int)N;++i)
typedef long long ll;
using namespace std;
int main()
{
	ll N,K=0,ANS=0;
	cin>>N;
	for(ll i=1;;i++){
		K=K+N;
		if(K%360==0){
			ANS=i;
			break;
		}
	}
	cout<<ANS<<endl;
}

Measured memory_bytes: 3188