akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <fstream>

#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair

#define NMAX 2000001

int viz[NMAX];

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    // cout.setf(ios::fixed), cout.precision(20);

#ifdef LOCAL
    // ifstream cin("input.txt");
#endif

    int k;
    cin >> k;

    int rest = 0, sol = -1;
    for (int i = 1; i <= NMAX - 1; i++) {
        rest = (rest * 10 + 7 % k) % k;
        if (rest == 0) {
            sol = i;
            break;
        }
        if (viz[rest]) {
            break;
        }
        viz[rest] = 1;
    }
    cout << sol << "\n";

    return 0;
}

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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<ll,ll>;

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

int N;
ll K;
P ps[100100];
ll acc[100100];

ll score(ll x){
    ll res = 0ll;
    
    for(int i=0;i<N;++i){
        if((x & fst(ps[i])) == fst(ps[i])){
            res += snd(ps[i]);
        }
    }

    return res;
}

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

    std::cin >> N >> K;

    for(int i=0;i<N;++i){
        std::cin >> fst(ps[i]) >> snd(ps[i]);
    }

    for(int i=0;i<N;++i){
        acc[i] += snd(ps[i]);
        if(i + 1 < N){
            acc[i+1] += acc[i];
        }
    }

    ll res = score(K);
    
    for(int i=0;i<=32;++i){
        if(K >> i & 1){continue;}
        for(int j=i+1;j<=32;++j){
            if(!(K >> j & 1)){continue;}

            ll x = (K | ((1ll << (i + 1)) - 1)) ^ (1ll << j);
            res = max(score(x), res);
        }
    }

    std::cout << res << std::endl;
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
typedef pair<int,int> P;
struct edge{int to, id;};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod 1000000007

using graph = vector<vector<ll>>;

int main(){
   ll n;
   cin >> n;
   char hoge[n];
   ll red,white;
   red = 0; white = 0;
   rep(i,n){
       char temp;
       cin >> temp;
       hoge[i] = temp;
       if(temp == 'R') red++;
       else white++;
    }
    ll ans = n+100;
    ll left,right;
    left = 0;right = red;
    ans = max(left,right);
    for(int i = 0; i < n; i++){
        ll huga;
        if(hoge[i] == 'R'){
            right--;
        }
        else{
            left++;
        }
        huga = max(left,right);
        ans = min(ans,huga);
    }
    ll piyo = max(white,(ll)0);
    ans = min(ans,piyo);
    cout << ans << endl;

}

Measured memory_bytes: 3800
N,K = [ int(it) for it in input().split() ]
S = [ int(it) for it in input() ]

li = []

v = 1
c = 0
for it in S:
  if (v != it ):
    li.append(c)
    c=1
  else:
    c+=1
  v = it
li.append(c)
s = 0
lis = [0]*(len(li)+1)
for i in range(len(li)):
  lis[i+1]=lis[i]+li[i]
s = 0
for i in range(N):
  if (i*2>=N):
    continue
  a = min(i*2,len(li))
  b = min(i*2+2*K+1,len(li))

  ds2 = lis[b]-lis[a]
  s = max(s,ds2)
print (s)


Measured memory_bytes: 49500
#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
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; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    cin.tie(0);
    ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう

*/

const ll mod = 1e9+7;

void chmod(ll &M){
    if(M >= mod) M %= mod;
    else if(M < 0){
        M += (abs(M)/mod + 1)*mod;
        M %= mod;
    }
}

ll modpow(ll x, ll n){
    if(n==0) return 1;
    ll res=modpow(x, n/2);

    if(n%2==0) return res*res%mod;
    else return res*res%mod*x%mod;
}

int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };


// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b){
    double ax=a.first, ay=a.second, bx=b.first, by=b.second;
    return atan2(by-ay, bx-ax);
}

/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */

int n, k;
vector<int> a;
vector<int> memo;
// 1:win, -1:lose, 0:unknown

int rec(int x){
    if(memo[x] != 0) return memo[x];
    if(x == 0) return -1;

    rep(i, n){
        if(x-a[i] >=0 && rec(x-a[i])==-1) return memo[x] = 1;
    }
    return memo[x] = -1;
}

void input(){
    cin >> n >> k;
    a.resize(n);
    rep(i, n) cin >> a[i];
    memo.resize(k+5, 0);
}

void solve(){
    if(rec(k) == 1) cout << "First" << endl;
    else cout << "Second" << endl;
}

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

    input();
    solve();
    

    return 0;
}

Measured memory_bytes: 5376
#include <bits/stdc++.h>
#include <algorithm>
// #include <atcoder/all>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
// using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
#define REP(i, m, n) for (ll(i) = (ll)(m); i < (ll)(n); ++i)
#define REP2(i, m, n) for (ll(i) = (ll)(n)-1; i >= (ll)(m); --i)
#define rep(i, n) REP(i, 0, n)
#define rep2(i, n) REP2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define rall(hoge) (hoge).rbegin(), (hoge).rend()
#define en '\n'
using Edge = pair<ll, ll>;
using Graph = vector<vector<Edge>>;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
// constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
template <class T> inline void print(T x) { cout << x << '\n'; }
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;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x,y)*y ;}
// ~~~~~~~~~~~~~~memo~~~~~~~~~~~~~~
// for (int i = 0; i < n; ++i)
// cout << res << endl;
// cout << res << '\n';
// int型の2次元配列(h×w要素の)の宣言
// vector<vector<int>> data(h, vector<int>(w));
// rotate(s.begin(), s.begin() + 1, s.end());
// sort(all(a),greater<int>());
// s.substr(0,2) 先頭から2文字切り出す
// for (auto it = mp.rbegin();it != mp.rend(); ++it)

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    ll res = 1;
    rep(i,n) {
        ll t;
        cin >> t;
        res = lcm(res,t);
    }
    print(res);
    

    return 0;
}


Measured memory_bytes: 3624
#include <bits/stdc++.h> 
using namespace std; 
#define ll long long
bool compare(const pair<int,int> &a,  
               const pair<int,int> &b) 
{ 
       return (a.second > b.second); 
}
int findmax(int arr[], int n){
	int curr_max = -1;
	for(int i=0;i<n;i++){
		curr_max = max(curr_max, arr[i]);
		}
	return curr_max;
	}
int findmin(int arr[], int n){
	int curr_max = INT_MAX;
	for(int i=0;i<n;i++){
		curr_max = min(curr_max, arr[i]);
		}
	return curr_max;
	}
bool help(int arr[], int mid, int n, int c){
	int last_pos = arr[0];
	int cows = 1;
	for(int i=0;i<n;i++){
		if(abs(arr[i]-last_pos) >= mid){
			last_pos = arr[i];
			if(++cows == c)
				return true;
			last_pos = arr[i];
		}
	}
	return false;
}
int main() 
{ 
	ll t;
	t=1;
	while(t--){
		ll n,W;
		cin>>n>>W;
		ll wt[n], val[n];
		for(int i=0;i<n;i++) cin>>wt[i]>>val[i];
		ll dp[n+1][W+1];
		for(int i=0;i<=n;i++){
			for(int w=0;w<=W;w++){
				if(i==0 or w==0)
					dp[i][w]=0;
				else if(wt[i-1]<=w){
					dp[i][w] = max(val[i-1] + dp[i-1][w-wt[i-1]], dp[i-1][w]);
					}
				else{
					dp[i][w] = dp[i-1][w];
				}
				}
			}
			cout<<dp[n][W];
	}
} 


Measured memory_bytes: 79104
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <cmath>

#define	MOD 1000000007;
#define PI 3.14159265358979323846;
using namespace std;
typedef long long ll;
ll mod = 1000000007;

int main()
{
	int a, b;
	cin >> a >> b;

	if (a % 2 == 0 && b % 2 == 1)
	{
		b++;
	}
	else if (a % 2 == 1 && b % 2 == 0)
	{
		a++;
	}

	cout << (a + b) / 2 << endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i,cc,n) for(int i=cc;i<=n;++i)
#define drep(i,cc,n) for(int i=cc;i>=n;--i)
typedef long long ll;
using namespace std;
int main(){
    int x,y;
    cin>>x>>y;
    cout<<x+y/2<<endl;
    return 0;
}

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

using namespace std;

typedef complex<double> P;

const double EPS = 1e-10;

double dot(P a, P b) { return real(conj(a)*b); }
double cross(P a, P b) { return imag(conj(a)*b); }

enum { COUNTER_CLOCKWISE, CLOCKWISE, ONLINE_BACK, ONLINE_FRONT, ON_SEGMENT };
int ccw(P c, P a, P b)
{
  a = a - c;
  b = b - c;
  if(cross(a, b) >  EPS) return COUNTER_CLOCKWISE;
  if(cross(a, b) < -EPS) return CLOCKWISE;
  if(dot(a, b) < -EPS)   return ONLINE_BACK;
  if(norm(a) < norm(b))  return ONLINE_FRONT;
  return ON_SEGMENT;
}
  

int main() {
  
  int n;
  while(cin >> n && n) {
    vector<P> p(n);
    for(int i=0; i<n; i++) {
      int x, y; cin >> x >> y;
      p[i] = P(x, y);
    }
    
    vector<bool> used(n);
    P curr(0, 0);
    double ans = 0.0;
    
    for(int i=0; i<n; i++) {
      int num = -1;
      for(int j=0; j<n; j++) {
	if(!used[j]) {
	  
	  if(num == -1) {
	    num = j;
	  } else {
	    int c = ccw(curr, p[num], p[j]);
	    if(c == COUNTER_CLOCKWISE || c == ON_SEGMENT) {
	      // テ・ツ?湘ィツァツ津」ツ?ョテ・ツキツョテ」ツ?古」ツつ暗」ツつ甘・ツーツ湘」ツ?陛」ツ?湘」ツ??」ツつ暗」ツつ甘・ツ、ツァテ」ツ?催」ツ?陛」ツ?古・ツーツ湘」ツ?陛」ツ??」ツつづ」ツ?ョテ」ツつ津ゥツ?クテヲツ環?
	      num = j;
	    }
	  }
	  
	}
      }
      ans += sqrt(norm(p[num]-curr));
      used[num] = true;
      curr = p[num];
    }
    printf("%.1f\n", round(ans*10)/10.0);
  }
  return 0;
}

Measured memory_bytes: 1252
#include<iostream>

using namespace std;

int main(){
    int n, tm;
    cin >> n >> tm;
    int t[n];
    for(int i=0;i<n;i++)cin >> t[i];

    long long x = 0;

    for(int i=0;i<n-1;i++){
        if(t[i+1]-t[i]>tm)x += tm;
        else x += t[i+1]-t[i];
    }

    cout << x+tm << endl;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <set>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main()
{
  int n;
  cin >> n;
  string s;
  set<string> st;
  REP(i, n) {
    cin >> s;
    st.insert(s);
  }
  cout << (st.size() == 4 ? "Four" : "Three") << endl;
}


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

#define For(i, l, r) for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Rep(i, r) for (register int i = (0), i##end = (int)(r); i < i##end; ++i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
	int x(0), sgn(1); char ch(getchar());
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
	for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
	return x * sgn;
}

void File() {
#ifdef zjp_shadow
	freopen ("C.in", "r", stdin);
	freopen ("C.out", "w", stdout);
#endif
}

const int Mod = 1e9 + 7;

int n, m;

int main() {

	File();

	n = read(); m = read();
	if (abs(n - m) > 1) return puts("0"), 0;

	int res = 1 + (n == m);
	For (i, 1, n) res = 1ll * res * i % Mod;
	For (i, 1, m) res = 1ll * res * i % Mod;
	printf ("%d\n", res);

	return 0;

}

Measured memory_bytes: 256
inf = 10**9


def calcDistance(p1,p2):
  return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])

def calcNearestCheckpoints(person,checkpoints):
  ans = -1
  min_distance = inf
  for i in range(len(checkpoints)):
    distance = calcDistance(person,checkpoints[i])
    if distance < min_distance:
      ans = i+1
      min_distance = distance

  return ans

def main():
  N,M = map(int,input().split())
  people      = [list(map(int,input().split())) for _ in range(N)]
  checkpoints = [list(map(int,input().split())) for _ in range(M)]
  for i in range(N):
    person = people[i]
    print(calcNearestCheckpoints(person,checkpoints))

main()


Measured memory_bytes: 9216
a,v = map(int,input().split())
b,w = map(int,input().split())
t = int(input())

if a<b:
  if v>=w:
    if b-a<=(v-w)*t:
      print('YES')
      exit()
  print('NO')
else:
  if v>=w:
    if a-b<=(v-w)*t:
      print('YES')
      exit()
  print('NO')


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

using namespace std;


// dp[i]ÍiÜÅÌ}g[VJÅÜÞ±ÆÌÅ«éÅåÌ}g[VJ
int dp[201];
int n,m;
vector<pair<int,int> > mt;

int dfs(int idx){
	if(dp[idx]!=-1)
		return dp[idx];
	int maxMat=0;
	for(int i = 0; i < mt.size(); i++){
		if(mt[i].first<mt[idx].first&&mt[i].second<mt[idx].second){
			maxMat=max(maxMat,dfs(i)+1);
		}
	}

	dp[idx]=maxMat;
	return dp[idx];
}

int main(){

	while(cin>>n&&n!=0){
		mt.clear();
		fill(dp,dp+201,-1);
		for(int i = 0; i < n; i++){
			int h,r;
			cin>>h>>r;
			mt.push_back(make_pair(h,r));
		}
		cin>>m;
		for(int i = 0; i < m; i++){
			int h,r;
			cin>>h>>r;
			mt.push_back(make_pair(h,r));
		}
		int maxMat=0;
		for(int i = 0; i < mt.size(); i++){
			maxMat=max(maxMat,dfs(i));
		}
		cout<<maxMat+1<<endl;
	}

	return 0;
}

Measured memory_bytes: 0
import java.util.Scanner;
import java.util.Stack;

/**
 * https://abc043.contest.atcoder.jp/tasks/abc043_b
 */
public class Main {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		sc.close();

		Stack<Character> stack = new Stack<>();
		
		for(char c:s.toCharArray()){
			if(c=='B'){
				if(!stack.isEmpty()) stack.pop();
			}else{
				stack.push(c);
			}
		}
		
		StringBuilder ans = new StringBuilder();
		for(char c:stack){
			ans.append(c);
		}
		
		System.out.println(ans);
		
	}

}


Measured memory_bytes: 23764
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
	if(a%b==0)return b;
	else return gcd(b,a%b);
}
set<int>a;
int n,x,k;
int main()
{
	cin>>n;
	while(n--)
	{
		set<int>b;
		b.clear();
		swap(a,b);
		cin>>x;
		for(set<int>::iterator it=b.begin();it!=b.end();it++)
			a.insert(gcd(*it,x));
		a.insert(k);
		k=gcd(k,x);
	}
	int ans=1;
	for(set<int>::iterator it=a.begin();it!=a.end();it++)
	{
		ans=max(ans,*it);
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;
#define mod 1000000007
int main()
{
    int n,m;
    cin >> n >> m;
    int f[n][m];
    bool visited[n][m];
    char c;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            f[i][j]=0;
            cin >> c;
            if(c=='#')
            {
                visited[i][j]=1;
            }
            else
            {
                visited[i][j]=0;
            }
        }
    }
    if(!visited[0][0])f[0][0]=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!visited[i][j])
            {
                if(i-1>=0)
                {
                    f[i][j]+=f[i-1][j];
                    f[i][j]%=mod;
                }
                if(j-1>=0)
                {
                    f[i][j]+=f[i][j-1];
                    f[i][j]%=mod;
                }
            }
        }
    }
    cout << f[n-1][m-1];
    return 0;
}


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

int selectionSort(int A[], int N) {
  int count = 0;
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[minj] > A[j]) {
        minj = j;
      }
    }
    if (minj != i) {
      swap(A[minj], A[i]);
      count++;
    }
  }
  return count;
}

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int count = selectionSort(A, N);
  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i != N - 1) cout << " ";
  }
  cout << endl;
  cout << count << endl;
  return 0;
}


Measured memory_bytes: 3108
n=int(input())
p=list(map(int, input().split()))
count=0

for i in range(n-2):
  maximum=max(p[i],p[i+1],p[i+2])
  minimum=min(p[i],p[i+1],p[i+2])
  sumation=p[i]+p[i+1]+p[i+2]
  if p[i+1]==sumation-maximum-minimum:
    count=count+1
    
print(count)

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

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)

#define CHMIN(a, b) (a) = min((a), (b))
#define CHMAX(a, b) (a) = max((a), (b))

#define DEBUG(x) cout << #x << ": " << (x) << endl
#define PRINT(x) cout << x << endl
#define all(x) x.begin(),x.end()


int main() {
    int N,K;
    cin >> N >> K;
    cout << N-K+1 << endl;
}



Measured memory_bytes: 256
#include <stdio.h>
int main(void){
  int n,i,ans = 0;
  scanf("%d",&n);
  
  for(i=1; i<=9; i++){
      if(n%i == 0 && 1 <= n/i && n/i <= 9){
          ans = 1;
      }
  }
  
  if(ans == 1){
      printf("Yes\n");
  }else{
      printf("No\n");
  }
 
  return 0;
}

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

using namespace std;

int main(){
    int n,m,x,ans = 0,temp = 0;
    cin >> n >> m >> x;
    vector<int>v(n+1,0);

    for(int i = 0;i < m;i++){
        int a;
        cin >> a;
        v[a] = 1;
    }

    for(int i = 0;i <= x;i++){
        ans += v[i];
    }
    for(int i = x;i <= n;i++){
        temp += v[i];
    }

    cout << min(ans,temp) << endl;


    return 0;
}

Measured memory_bytes: 256
// {{{
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <complex>
#include <vector>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for(ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++)
#define FORR(i, a, b) for(ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--)
#define REP(i, n) for(ll i = 0ll; i < static_cast<ll>(n); i++)
#define REPR(i, n) for(ll i = static_cast<ll>(n); i >= 0ll; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> IP;
typedef pair<ll, LP> LLP;

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

constexpr int INF = 100000000;
constexpr ll LINF = 10000000000000000ll;
constexpr int MOD = static_cast<int>(1e9 + 7);
constexpr double EPS = 1e-9;
// }}}

int H, W, D, Q;
int a[301][301];
P aa[90000];
P p[100000];
int m[900000];

void init()
{
}

void solve()
{
    REPR(i, H*W){
        if(i+D <= H*W){
            m[i] = m[i+D] + abs(aa[i+D].fi-aa[i].fi) + abs(aa[i+D].se-aa[i].se);
        }
    }
    REP(i, Q){
        cout << m[p[i].fi] - m[p[i].se] << endl;
    }
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> H >> W >> D;
    FOR(i, 1, H+1){
        FOR(j, 1, W+1){
            cin >>a[i][j];
            aa[a[i][j]] = mp(i, j);
        }
    }
    cin >> Q;
    REP(i, Q){
        cin >> p[i].fi >> p[i].se;
    }
    solve();
    return 0;
}

// vim:set foldmethod=marker:


Measured memory_bytes: 4864
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
using P = pair<int,int>;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}




int main()
{
    int N;
    cin >> N;
    vector<vector<int>> xy(N);
    rep(i,N){
        int x,y;
        cin >> x >> y;
        xy[i].push_back(x);
        xy[i].push_back(y);
    }

    sort(xy.begin(),xy.end());
    double sum = 0;
    double j = 0;
    do{
        rep(i,N - 1){
            sum += sqrt(pow((xy[i][0] - xy[i+1][0]),2) + pow((xy[i][1] - xy[i+1][1]),2));
        }
        j++;
    }while(next_permutation(xy.begin(),xy.end()));

    double ans = sum/j;
    cout << fixed << setprecision(8) << ans << endl;

    return 0;
}
//Ctrl+Shift+Bでコンパイルと実行を行ってデバッグすること

Measured memory_bytes: 256
#!/usr/bin/env python3
import itertools

def main():
    N = int(input())
    F = [int(input().replace(' ',''),2) for _ in range(N)]
    P = [list(map(int,input().split())) for _ in range(N)]
    answer = -float('inf')

    for bit in range(1,1<<10):
        tmp_profit = 0
        for i in range(N):
            count = format(bit&F[i],'b').count("1")
            tmp_profit += P[i][count]
        answer = max(answer,tmp_profit)
    print(answer)
    return 

if __name__ == '__main__':
    main()



Measured memory_bytes: 3188
def get_score(x):
    result = 0
    if (x % 2) != 0:
        return 0

    while ((x % 2) == 0) and (x >= 2):
        x = x//2
        result = result + 1

    return result


n = int(input())
a = [int(i) for i in input().split()]

r = 0

for i in a:
    r = r + get_score(i)

print(r)


Measured memory_bytes: 4148
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
using namespace std;
#define INF 1000000007
#define LINF 1000000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
#define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X) \
    {          \
    }
#define ARDEB(i, X) \
    {               \
    }
#define END \
    {       \
    }
#endif
typedef pair<int, int> P;
struct edge{int to,cost;};

typedef long long ll;

int ans,ans2;
int  b[211111];
signed main(){

	int n,a[211111];
	cin>>n;
	
	rep(i,n){
		cin>>a[i];
		b[i]=a[i];
	}
	sort(a,a+n);
	int max=a[n-1],max2=a[n-2];
	rep(i,n){
		if(b[i]!=max)cout<<max<<endl;
		else cout<<max2<<endl;
	}
	
	
}


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

int main(){
    int a, b, c, x, y;
    cin >> a >> b >> c >> x >> y;
    
    int ans = 0;
    
    //no1
    int temp1 = a * min(x, y) + b * min(x, y);
    int temp2 = c * 2 * min(x, y);
    
    ans += min(temp1, temp2);
    
    int memo = min(x, y);
    x -= memo;
    y -= memo;
    
    //cout << x << y << endl;
    int temp3 = a * x +  b * y;
    int temp4 = c * 2 * max(x, y);
    
    ans += min(temp3, temp4);
    
    cout << ans << endl;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n;i>=0;i--)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;


int main(void){
    int n;
    cin >> n;
    string c;
    cin >> c;
    int sumr=0,sumw=0;
    REP(i,n){
        if(c[i]=='R')
            sumr++;
        else sumw++;
    }
    int a=0,b=n-1,sums=0;
    while(b>a){
        while(c[a]!='W' && a<n){
            a++;
        }
        while(c[b]!='R' && b>=0){
            b--;
        }
        if(b>a){
            swap(c[a],c[b]);
            sums++;
        }
    }

    cout << min(sumr,min(sumw,sums)) << endl;

    return 0;
}

Measured memory_bytes: 3728
#include <iostream>
#include<vector>
#include <cstring>
#include <cstdio>
#include<cstdint>
#include<utility>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
typedef pair<double,double>P_D;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define reprev(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define MAX 100100
#define MAX_N 200100
#define SENTINEL 2000000000
const int MOD = 1e9+7;
#define NIL -1
const int INF = 1 << 29;
string S,T;
int n,m;
bool func(string question,string answer){
    if(question.size()!=answer.size())return 0;
    rep(i,question.size()){
        if(question[i]==answer[i])continue;
        if(question[i]=='?')continue;
        if(question[i]!=answer[i])return 0;
    }
    return 1;
}
string solve(){
    bool b=false;
    bool flag=false;
    string p;
    rep(i,n-m+1){
        b=func(S.substr(i,m),T);
        if(b){
            flag=true;
            p=S.substr(0,i)+T+S.substr(i+m,n-m-i+1);
        }
    }
    if(!flag)return "UNRESTORABLE";
    rep(i,n){
        if(p[i]=='?'){
            p[i]='a';
        }
    }
    return p;
    
}
int main(){
    cin>>S>>T;
    n=S.size();m=T.size();
    cout<<solve()<<endl;
}



Measured memory_bytes: 256
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
# stdin = open("sample.txt")

MOD = 1000000007

def keta(kazu):
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def combination(m,n):
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x
    
def main():
    a, v = [int(x) for x in stdin.readline().rstrip().split()]
    b, w = [int(x) for x in stdin.readline().rstrip().split()]
    t = int(stdin.readline().rstrip())
    start = abs(a-b)
    if t*v - t*w >= start:
        print("YES")
    else:
        print("NO")
    
main()

Measured memory_bytes: 9976
using DataStructures
const double = Float64
const int = Int64

parseint(x) = parse(int, x)
readint() = map(parseint, split(readline()))

function main()
  A,G,B = readint()
  K = readint()[1]

  flag = false
  for i in 1:K
    if G <= A 
      G *=2
    else
      B *=2
    end
    if A < G && G < B 
      flag = true
      break
    end
  end
  if flag 
    println("Yes")
  else
    println("No")
  end

  

end

main()

Measured memory_bytes: 185076
#include <iostream>
using namespace std;
int main(){
  int h,w;cin>>h>>w;
  int n;cin>>n;
  int a[n];
  for(int i = 0; n > i; i++){
    cin>>a[i];
  }
  int nw = 0;
  int ans[h][w];
  for(int i = 0; h > i; i++){
    if(i%2){
      for(int j = 0; w > j; j++){
        if(!(a[nw]))nw++;
        ans[i][j] = nw+1;
        a[nw]--;
      }
    }else{
      for(int j = w-1; 0<= j; j--){
        if(!(a[nw]))nw++;
        ans[i][j] = nw+1;
        a[nw]--;
      }
    }
  }
  for(int i = 0; h > i; i++){
    for(int j = 0; w > j; j++){
      cout << ans[i][j];
      if(j+1!=w)cout << " ";
    }
    cout << endl;
  }
}

Measured memory_bytes: 384
#include <iostream>

using namespace std;

int main()
{
	int t, n;
	int s, f;
	
	while (cin >> t, t){
		cin >> n;
		for (int i = 0; i < n; i++){
			cin >> s >> f;
			t -= (f - s);
		}
		if (t > 0)	cout << t;
		else	cout << "OK";
		cout << endl;
	}
	return (0);
}

Measured memory_bytes: 1160
N = int(input())
p = list(map(int, input().split()))
p_T = []
for i, v in enumerate(p, 1):
    if i == v:
        p_T.append(True)
    else:
        p_T.append(False)

cnt = 0
i = 0
while i < N:
    if p_T[i]:
        cnt += 1
        i += 2
    else:
        i += 1
print(cnt)


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

//対応表

//8進数変換
void eight(int n){
	int table[] = {0,1,2,3,5,7,8,9};
	if(n >= 8){
		eight(n / 8);
	}
	cout << table[n % 8];
}

int main(){
	int num;
	while(cin >> num,num){
		eight(num);
		cout << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
#include<stdio.h>

int main(){
	int N,sum=0,max=0;
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		int p;
		scanf("%d",&p);
		sum+=p;
		if(p>max)max=p;
	}
	sum-=max/2;
	printf("%d\n",sum);
	return 0;
}

Measured memory_bytes: 128
s=str(input())
t=str(input())
a=len(s)
b=len(t)
c=a-b
list=[]
for i in range(c+1):
  ans=0
  for j in range(b):
    if s[i+j]==t[j]:
      ans+=1
  list.append(ans)
  ans=0

print(b-max(list))

Measured memory_bytes: 9088
from collections import deque

H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]

cnt_white = 0
for path in field:
    cnt_white += path.count('.')

sy, sx = 0, 0
gy, gx = H - 1, W - 1

dyx = [(1, 0), (-1, 0), (0, 1), (0, -1)]
q = deque()
q.append((sy, sx, 0))

while q:
    cy, cx, ct = q.popleft()
    for dy, dx in dyx:
        ny, nx = cy + dy, cx + dx
        if ny < 0 or ny >= H or nx < 0 or nx >= W:
            continue
        if (ny, nx) == (gy, gx):
            print(cnt_white - (ct + 2)) # ct + 2 が使用した '.' の個数
            exit()
        if field[ny][nx] == '#':
            continue
        q.append((ny, nx, ct + 1))
        field[ny][nx] = '#'

print('-1')

Measured memory_bytes: 40176
def getIntList():
    return list(map(int, input().split()))    

N, K = getIntList()

zx = getIntList()
res = 10000000000
for i in range(N-K+1):
    a = zx[i]
    b = zx[i+K-1]
    if  a*b <0:
        a = abs(a)
        b = abs(b)
        t = min(a,b)*2 + max(a,b)
    else:
        a = abs(a)
        b = abs(b)
        t = max(a,b)
    res = min(res,t)
print(res)

Measured memory_bytes: 14228
#include <bits/stdc++.h>
using namespace std;
int c[5][5];
int main(){
  int i,j,count=0;
  for(i=0;i<3;i++){
    for(j=0;j<3;j++){
      scanf("%d",&c[i][j]);
    }
  }
  for(i=0;i<3;i++){
    c[3][i]=c[0][i];
    c[i][3]=c[i][0];
    c[4][i]=c[1][i];
    c[i][4]=c[i][1];
  }
  c[3][3]=c[0][0];
  for(i=0;i<3;i++){
    for(j=0;j<3;j++){
      //    printf("%d,%d,%d,%d\n",c[i][j]-c[i][j+1],c[i+1][j]-c[i+1][j+1],c[i][j]-c[i+1][j],c[i][j+1]-c[i+1][j+1]);
      if(c[i][j]-c[i][j+1]!=c[i+1][j]-c[i+1][j+1])count++;
      if(c[i][j]-c[i+1][j]!=c[i][j+1]-c[i+1][j+1])count++;
    }
  }
  if(count==0)printf("Yes\n");
  else printf("No\n");
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int a,b;
  while(cin>>a>>b){
    int d[105]={};
    int i;
    d[a]++;d[b]++;
    if(a==0&&b==0) {
      cout << "OK" << endl;
      continue;
    }
    while(cin>>a>>b,a||b){
      d[a]++;d[b]++;
    }
    bool f=true;
    for(i=3;i<105;i++){
      if(d[i]%2!=0) f=false;
    }
    if(d[1]%2==1&&d[2]%2==1&&f) cout << "OK" << endl;
    else cout << "NG" << endl;
  }
  return 0;
}

Measured memory_bytes: 1156
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  V<> a(n); for (auto&& e : a) cin >> e;
  sort(rbegin(a), rend(a));
  a.push_back(0);
  int x = 0;
  while (true) {
    if (a[x + 1] - 1 <= x and x < a[x]) break;
    ++x;
  }
  bool res;
  if (x >= a[x + 1]) {
    res = !(a[x] - x & 1);
  } else if (x == a[x + 1] - 1) {
    bool f = !(a[x] - x & 1);
    int y = upper_bound(begin(a), end(a), a[x + 1], greater<int>()) - begin(a);
    bool g = !(y - x & 1);
    res = f or g;
  } else {
    int y = upper_bound(begin(a), end(a), a[x], greater<int>()) - begin(a);
    res = !(y - x & 1);
  }
  cout << (res ? "First" : "Second") << '\n';
}

Measured memory_bytes: 1024
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<vector>
#include<map>



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

long long int n;
long long int ans=10000000000000;
int main(){
    cin >> n;
    long long int i,j;
    for (int i=1;(long long int)i*i<=n;i++){
        if(n%i == 0){
            ans = min(ans , i+n/i-2);
        }
    }
    cout << ans << endl;

return 0;
}

Measured memory_bytes: 3636
#include <algorithm>
#include <iostream>
#include <vector>
#define int int64_t
using namespace std;
main() {
    int X,Y,Z,K; cin>>X>>Y>>Z>>K;
    vector<int> A(X); for(auto& x:A) cin>>x;
    vector<int> B(Y); for(auto& x:B) cin>>x;
    vector<int> C(Z); for(auto& x:C) cin>>x;
    vector<int> E;
    for(auto a:A) for(auto b:B) E.push_back(a+b);
    sort(E.begin(), E.end());
    reverse(E.begin(),E.end());
    E.resize(K);
    vector<int> EC;
    for(auto e:E) for(auto c:C) EC.push_back(e+c);
    sort(EC.begin(), EC.end());
    reverse(EC.begin(),EC.end());
    for(int k=0; k<K; ++k) cout<<EC[k]<<endl;
}

Measured memory_bytes: 43368
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string s;
char c[256];
int main(void){
  cin >> s;
  for(int i = 0; i < (int)s.size(); i++){
    c[s[i]] = 1;
  }
  if((c['N']+c['S'])%2 || (c['W']+c['E'])%2)
    cout << "No" << endl;
  else
    cout << "Yes" << endl;
}


Measured memory_bytes: 256
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>
#include <iomanip>

using namespace std;

#define FOR(i, a, b) for(int (i)=(a);(i)<(b);(i)++)
#define IFOR(i, a, b) for(int (i)=(a);(i)<=(b);(i)++)
#define RFOR(i, a, b) for(int (i)=(a);(i)>=(b);(i)--)
#define REP(i, n) FOR((i), 0, (n))

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class T>
void print_container(T &container, string delim=" ") {
    bool first = true;
    for(auto &t: container){
        if(!first){
            cout << delim;
        }
        first = false;
        cout << t;
    }
    cout << endl;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<bool> snuke(n, false);
    REP(i, k){
        int d;
        cin >> d;
        REP(j, d){
            int a;
            cin >> a;
            a--;
            snuke[a] = true;
        }
    }

    int cnt = 0;
    REP(i, n){
        if(!snuke[i]) cnt++;
    }

    cout << cnt << endl;

    return 0;
}

Measured memory_bytes: 3200
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<functional>
#include<map>
#include<cstdlib>
#include<complex>
#include<numeric>
#include<set>
#include<deque>
#include<queue>

using namespace std;
using Int = long long;
Int INF = 1LL << 60;
const Int MOD = 1000000000 + 7;

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Int N;
	cin >> N;

	if (N%2) {
		cout << 0 << "\n";
	}
	else {
		Int ans = 0;
		Int temp = 5;
		/*for (Int i = 0; i < 18; i++) {
			ans += N / temp;
			temp *= 10;
		}*/
		Int temp2;
		while (temp<=N) {
			temp2 = N / temp;
			temp2 /= 2;
			ans += temp2;
			temp *= 5;

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

	}

}


Measured memory_bytes: 256
#include<stdio.h>
int main()
{
	int n,k,p,i,stone;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&k,&p);
		stone=k%p;
		if(stone==0)
		stone=p;
		printf("%d\n",stone);
	}	
	return 0;
}


Measured memory_bytes: 2112
program atcoder_express
  implicit none
  integer :: n, i
  real(8) :: t(100), v(0:101), l(0:101), r(0:101)
  real(8) :: x, f0, f1, s, ds
  character(32) :: a
  t = 0.d0
  v = 0.d0
  l = 0.d0
  r = 0.d0
  read(*,*) n
  read(*,*) t(1:n)
  read(*,*) v(1:n)
  do i = 1, n
    l(i) = r(i-1)
    r(i) = l(i) + t(i)
  end do
  l(n+1) = r(n)
  r(n+1) = l(n+1)
  x = 0.d0
  f0 = 0.d0
  s = 0.d0
  ds = 0.5d0
  do
    f1 = f(s+ds,n,l(0:n+1),r(0:n+1),v(0:n+1))
    x = x + 0.5d0*(f0+f1)*ds
    f0 = f1
    s = s + ds
    if (s.ge.r(n+1)) exit
  end do
  write(a,'(f22.10)') x
  write(*,'(a)') trim(adjustl(a))
  stop
contains
  function f(t,n,l,r,v) result(u)
    implicit none
    integer, intent(in) :: n
    real(8), intent(in) :: t, l(0:n+1), r(0:n+1), v(0:n+1)
    integer :: i
    real(8) :: u, w
    u = 1.e20
    do i = 0, n+1
      w = v(i) + max(l(i)-t,0.d0) + max(t-r(i),0.d0)
      if (u.gt.w) u = w
    end do
  end function f
end program atcoder_express

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

int main() {
	int h, w;
	while (cin >> h >> w, h) {
		rep(i, h) {
			rep(j, w) {
				if (i == 0 || i == h - 1 || j == 0 || j == w - 1)cout << "#";
				else cout << ".";
			}
			cout << endl;
		}
		cout << endl;
	}
}

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
int main(){
	int x,t;
	cin>>x>>t;
	cout<<max(x-t,0)<<endl;
}

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

#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long  INF=1000000000000000000;
long long mod=1000000007;


int main(){
    int h,w;
    cin>>h;
    cin>>w;
    int N;
    cin>>N;

    int p=max(h,w);

    cout<<ceil((double)N/p)<<endl;
    
    return 0;

}
/*


*/


Measured memory_bytes: 256
#include<stdio.h>
int main(){
  while(1){
    int a,b;
    scanf("%d%d",&a,&b);
    if(a==0 && b==0){
      break;
    }else{
      if(a<b){
      printf("%d %d\n",a,b);
    }else{
      printf("%d %d\n",b,a);
    }
    }
  }
  return 0;
}



Measured memory_bytes: 2676
N, K = map(int, input().split())
*V, = map(int, input().split())
ans = 0

for l in range(K + 1):
	for r in range(K - l + 1):
		inHand = []
		if l + r > N:  # Nを超えてしまうありえない場合を処理
			break
		# inHandに左からl個、右からr個の要素を追加
		inHand = V[: l]
		inHand.extend(V[N - r:])
		tmp = sum(inHand)  # 現時点でのinHandの和をtmpとして負の要素をtmpから引いていく
		# inHand中の負の要素を消せるだけ消す
		inHand.sort()
		for i in range(min(K - l - r, l + r)):
			if inHand[i] >= 0:
				break
			tmp -= inHand[i]
		ans = max(ans, tmp)

print(ans)

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

using namespace std;

int main()
{

        string n;
        getline(cin,n);
        int sizen = n.size();
        stack<long long int>st;
        for(int i=0; i<sizen; i++)
        {
            if(n[i]==' ')
                continue;
            else if(n[i]=='+' || n[i]=='-' || n[i]=='*')
            {
                long long int p,q;
                p = st.top();
                st.pop();
                q = st.top();
                st.pop();

                if(n[i]=='+')
                {
                    st.push(p+q);
                }
                else if(n[i]=='-')
                {
                    st.push(q-p);
                }
                else if(n[i]=='*')
                {
                    st.push(p*q);
                }
            }
            else
            {
                long long int x = 0;
                while(1)
                {
                    if(n[i] ==' ' || i==sizen)
                        break;
                    x += (int) n[i] - 48;
                    x = x * 10;
                    i++;
                }
                x = x/10;
                i--;
                st.push(x);
            }
        }
        cout<<st.top()<<endl;

}



Measured memory_bytes: 3160
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF= 1e+18+1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll> >vvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
const ll MOD=1000000007LL;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(){
  vl vec={0,2,5,5,4,5,6,3,7,6};
  ll n,m;cin>>n>>m;
  ll dp[10010];
  rep(i,10010)dp[i]=-INF;
  dp[0]=0;
  vl a(m);
  rep(i,m){
    cin>>a[i];
    rep(j,n+1){
      if(j<vec[a[i]])continue;
      dp[j]=max(dp[j],dp[j-vec[a[i]]]+1);
    }
  }
  ll num=dp[n];
  sort(a.begin(),a.end());
  reverse(a.begin(),a.end());
  ll count=0;
  while(count<num&&n>0){
    rep(i,m){
      if(n-vec[a[i]]>=0&&dp[n]==dp[n-vec[a[i]]]+1){
        n-=vec[a[i]];
        count++;
        cout<<a[i];
        break;
      }
    }
  }
 // cout<<endl;
  //cout<<vec[1]<<" "<<vec[9]<<endl;

}

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

class C {
    public static void Main() {
        Console.ReadLine();
        var a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        var average = (int)Math.Round(a.Average());
        Console.WriteLine(a.Sum(k => (k - average) * (k - average)));
    }
}


Measured memory_bytes: 13396
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define dep(i,a,b) for(int i = a; i >= b; i--)
#define Rep(i,a) for(int i = 0; i < a; i++)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define x first
#define y second
#define pb(a) push_back(a)
const int N = 2e5 + 10, M = 1e8;
pii a[N];
int s[N];
const double pi = acos(-1.0);


int main() {
	static int n, x1, y1, x2, y2, f[N];
	scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
	scanf("%d",&n);
	rep(i,1,n) scanf("%d%d", &a[i].x, &a[i].y);
	
	if (x1 > x2) swap(x1, x2), swap(y1, y2);
	if (y1 > y2) {
		y1 = M - y1, y2 = M - y2;
		rep(i,1,n) a[i].y = M - a[i].y;
	}
	sort(a + 1, a + n + 1);

	int ans = 0;
	s[0] = 0; rep(i,1,n) s[i] = M + 1; 
	rep(i,1,n) if (a[i].x >= x1 && a[i].y >= y1) {
		int l = 0, r = i;
		while (l + 1 < r) {
			int mid = (l + r) >> 1;
			if (s[mid] <= a[i].y) l = mid; else r = mid;
		}
		f[i] = max(f[i], l + 1);
		s[l + 1] = a[i].y;
		if (a[i].x <= x2 && a[i].y <= y2) ans = max(ans, f[i]);
	}
	if (ans == min(x2 - x1, y2 - y1) + 1) 
		printf("%.20lf\n", (x2 - x1 + y2 - y1) * 100LL - 20 * (ans - 1) * (1 - pi / 4) + 20 * (pi / 2 - 1));
	else
		printf("%.20lf\n",(x2 - x1 + y2 - y1) * 100LL - 20 * ans * (1 - pi / 4));
	return 0;
}


Measured memory_bytes: 3328
#include <bits/stdc++.h>
using namespace std;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	long long int a[200001],z=0,k;
	bool l=1;
	long long int n;
	cin>>n;
	k=n-2;
	for(long long int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    z=a[n-1];
    for(long long int i=0;i<n-2;i++)
    {
        if(l)
        {
            z+=a[k];
            l=0;
        }
        else
        {
            z+=a[k];
            k--;
            l=1;
        }
    }
    cout<<z<<endl;
	return 0;
}


Measured memory_bytes: 5192
#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<cstdint>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

int main() {
  std::cin.tie(0); std::ios::sync_with_stdio(false);
  std::cout << std::setprecision(10) << std::fixed;
  int N, p = -1; std::cin >> N;
  std::vector<int64_t> A(N), B(N), C(N+1);
  int64_t ma = -1;
  for(int i = 0; i < N; ++i) {
    std::cin >> A[i];
    if(A[i] > ma) ma = A[p=i];
  }
  for(int i = 0; i < N; ++i) std::cin >> B[i];
  C[0] = C[1] = 0;
  for(int i = 1; i < N; ++i) C[i+1] = C[i] + 2*B[(i+p)%N];
  for(int i = 1; i <= N; ++i) C[i] += C[i-1];
  auto slope = [&](int i, int j)->double {
    return (double)((A[(j+p)%N] - C[j]) - (A[(i+p)%N] - C[i]))/(j-i);
  };
  std::vector<int> hull{0,1};
  double last = slope(0, 1);
  for(int j = 2; j <= N; ++j) {
    while(slope(hull.back(), j) > last) {
      hull.pop_back();
      if(hull.size() < 2) break;
      last = slope(*(hull.end()-2), hull.back());
    }
    hull.push_back(j);
    last = slope(*(hull.end()-2), hull.back());
  }
  double sum = 0.0;
  for(int i = 0; i < hull.size()-1; ++i) {
    int l = hull[i], r = hull[i+1];
    // at [l, r)
    double Fl = A[(l+p)%N] - C[l], Fr = A[(r+p)%N] - C[r];
    for(int j = l; j < r; ++j) {
      sum += (Fl*(r-j) + Fr*(j-l))/(r-l) + C[j];
    }
  }
  sum /= N;
  fin(sum);
  return 0;
}


Measured memory_bytes: 9304
#include<stdio.h>

int main(void){
    int n,T;
    scanf("%d%d", &n,&T);
    int t[n];
    for(int i=0; i<n; i++){
        scanf("%d", &t[i]);
    }

    int tt[n];
    for(int i=0; i<n; i++){
        tt[i] = t[i] + T;
    }


    int s[n];
    s[0] = T;
    int diff;
    for(int i=1; i<n; i++){
        diff = t[i] - tt[i-1];

        if(diff >= 0){
            s[i] = s[i-1] + T;
        }else{
            s[i] = s[i-1] + T + diff;
        }
    }

    printf("%d\n", s[n-1]);
    return 0;
}

Measured memory_bytes: 2560
import java.util.*;
import java.math.*;
import java.io.*;

public class Main{
  public static void main(String agrgs[]){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int arr[][]=new int[n][3];
    for(int i=0;i<n;i++){
      arr[i][0]=sc.nextInt();
      arr[i][1]=sc.nextInt();
      arr[i][2]=sc.nextInt();
    }
    int dp[][]=new int[n][3];
    dp[0][0]=arr[0][0];
    dp[0][1]=arr[0][1];
    dp[0][2]=arr[0][2];
    for(int i=1;i<n;i++){
     	dp[i][0]=Math.max(dp[i-1][1],dp[i-1][2])+arr[i][0];
     	dp[i][1]=Math.max(dp[i-1][0],dp[i-1][2])+arr[i][1];
     	dp[i][2]=Math.max(dp[i-1][0],dp[i-1][1])+arr[i][2];
    }
    System.out.println(Math.max(dp[n-1][0],Math.max(dp[n-1][1],dp[n-1][2])));
  }
}


Measured memory_bytes: 99072
/*** akshaykumar99 ***/
 
#include<bits/stdc++.h> 
using namespace std;
 
const long long MOD = 1e9+7;
const long double PI = acos(-1);
const long long N = 1e18+1;
 
typedef long long ll;
typedef long double ld;
ll binpow(ll a, ll b, ll m) 
{
    a %= m;
    ll res = 1;
    while (b > 0) 
    {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

ll inv(ll x)
{
    return binpow(x, MOD-2, MOD);
}

ll findProductSum(vector<ll> A, ll n) 
{ 
    ll array_sum = 0; 
    for (ll i = 0; i < n; i++) 
    {
        array_sum = (array_sum + A[i]) % MOD; 
    }
    ll array_sum_square = (array_sum * array_sum) % MOD; 
    ll individual_square_sum = 0; 
    for (ll i = 0; i < n; i++) 
    {
        individual_square_sum = (individual_square_sum + A[i]*A[i] % MOD) % MOD; 
    }
    ll ans = (array_sum_square - individual_square_sum + MOD) * inv(2) % MOD;
    return ans; 
}

void test_case()
{
    ll n;
    cin >> n;
    vector<ll> v(n);
    for(ll i = 0; i < n; i++)
    {
    	cin >> v[i];
    }
    cout << findProductSum(v, n) << '\n';

}
 
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL); 
    ll tc=1;
    // cin>>tc;
    while(tc--)
        test_case();
    return 0;
}


Measured memory_bytes: 6360