akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>

int main(void)
{
	int i, N, num, NA, not;
	
	while (scanf("%d", &N), N != 0){
		NA = 1;
		not = 1;
		
		for (i = 0; i < N; i++){
			scanf("%d", &num);
			if (num > 1){
				NA = 0;
			}
			if (num > 0){
				not++;
			}
		}
		
		if (NA == 1){
			printf("NA\n");
		}
		else {
			printf("%d\n", not);
		}
	}
	
	return (0);
}

Measured memory_bytes: 612
//小数点以下　cout << fixed << setprecision(10) << num << endl;
//桁数はlog10(mid)でやれ　log(mid)/log(10)はWA
//long*long<0で判定するな
//開区間と閉区間
//アルファベットが何番目かは x=s[i]-'a'
//bfsはmain()内で　rep(i,h)rep(j,w)rep(i,4)でバグ
//切り上げ：(a+(b-1))/b
//32bit以上は 1ull<<j
// map 初期値0 for(auto itr=mp.begin();itr!=mp.end();++itr) itr->fi itr->se で全探索
// for(auto u:mp) u.fi u.se も可能
//n個のgcd,lcmは一つずつみて更新していく
//大きな数を扱うときは素因数分解した形で持っておく
// (l/a[i]) = l*powmod(a[i],MOD-2)%MOD;
//auto itr=lower_bound(ALL(a),key) a[itr-a.begin()]
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <stdio.h>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional> //operator[]
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;

signed main(){
    int n; cin >> n;
    
    bool f[n][10];
    rep(i,n) rep(j,10) cin >> f[i][j];
    
    int p[n][11];
    rep(i,n)rep(j,11) cin >> p[i][j];
    
    ll ans=0;
    rep(bit,1<<10){
        if(bit==0) continue;
        vector<bool> joi(10,0);
        rep(i,10){
            if(bit&(1<<i)) joi[i]=1;
        }
        ll res=0;
        rep(i,n){
            int cnt=0;
            rep(j,10){
                if(f[i][j]&&joi[j]) cnt++;
            }
            res+=p[i][cnt];
        }
        chmax(ans,res);
        if(ans==0) ans=res;
    }
    cout << ans << endl;
    
}


Measured memory_bytes: 256
#include <iostream>
#include <climits>
#define INF (INT_MAX / 2)
using namespace std;

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

  int n;
  cin >> n;
  int f[n][5][2];
  for(int i = 0; i < n; i++){
    for(int j = 0; j < 5; j++){
      cin >> f[i][j][0];
      cin >> f[i][j][1];
    }
  }
  int p[n][11];
  for(int i = 0; i < n; i++){
    for(int j = 0; j <= 10; j++){
      cin >> p[i][j];
    }
  }

  int ans = -INF;
  for(int s = 1; s < (1 << 10); s++){
    int c[n];
    fill(c, c + n, 0);
    for(int i = 0; i < n; i++){
      for(int j = 0; j < 5; j++){
        for(int k = 0; k < 2; k++){
          if(f[i][j][k] == 1 &&
              (s & (1 << (2 * j + k))) != 0){
            c[i]++;
          }
        }
      }
    }
    int sum = 0;
    for(int i = 0; i < n; i++){
      sum += p[i][c[i]];
    }
    ans = max(ans, sum);
  }

  cout << ans << endl;

  return 0;
}

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

int x,y,z;

int main(){
	cin>>x>>y>>z;
	if(((x == y) && (y == z)) || ((x != y) && (y != z) && (x != z))){
		cout<<"No"<<endl;
	}else{
		cout<<"Yes"<<endl;
	}
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int MAXN=100010;
typedef long long ll;
int A[MAXN];
int n;

void init(int n) {
  for (int i=1; i<=n; i++) {
    A[i]=0;
  }
}
void add(int i,int x) {
  while (i<=n) {
    A[i]+=x;
    i+=i&(-i);
  }
}
ll getSum(int s,int t) {
  ll sumt=0;
  while (t) {
    sumt+=A[t];
    t-=t&(-t);
  }
  ll sums=0;
  s-=1;
  while (s) {
    sums+=A[s];
    s-=s&(-s);
  }
  ll sum=sumt-sums;
  return sum;
}
int main() {
  int q;
  cin >> n >> q;
  init(n);
  while (q--) {
    int com, x, y;
    cin >> com >> x >> y;
    if (com==0) {
      add(x,y);
    } else {
      cout << getSum(x,y) << endl; 
    }
  }
  return 0;
}

Measured memory_bytes: 3448
// finish date: 2019/11/24

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int (i)=a;(i)<(b);(i)++)
#define rep(i, n)  FOR(i,0,n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<vector<vector<double>>> vvvd;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, pair<int, int>> plii;
const int bigmod = 1000000007;
const int INF = 1000000007;
const long long INFll = 100000000000000000;


int main() {
    string S;
    cin >> S;
    map<string, int> m;
    m["SUN"] = 7;
    m["MON"] = 6;
    m["TUE"] = 5;
    m["WED"] = 4;
    m["THU"] = 3;
    m["FRI"] = 2;
    m["SAT"] = 1;
    cout << m[S] << endl;

    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <map>
#include <queue>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
#define inf 1 << 28
const int maxn = 1e5 + 5;
typedef long long ll;

int const N = 55557;
bool vis[N];
void isprime()
{
    memset(vis, false, sizeof vis);
    vis[1] = true;
    for (int i = 2; i < 55555; i++)
    {
        if (!vis[i])
        {
            for (int j = i + i; j < 55555; j += i)
            {
                vis[j] = true;
            }
        }
    }
}
int n;
void solv()
{
    cout << 7 <<' ';
    int sum=2,ans=1;
    for (int i=12;i<55555;i++)
    {
        if(!vis[i]&&i%10==7)
        {
            cout<<i<<' ';
            ans++;
        }
        if(ans==n)
        break;
    }
    cout<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    isprime();
    cin >> n;
    solv();
}

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

using namespace std;

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


Measured memory_bytes: 256
#include"bits/stdc++.h"
#define ll long long int
using namespace std;
int main()
{
  int n; cin>>n; 
  string s; cin>>s; 
  vector<int>v1(n,0),v2(n,0); 
  for(int i=0;i<=n-1;i++)
  {
   if(i==0)
   {
   	if(s[i]=='R'){ v1[0]=1; v2[0]=0; }
   	else { v1[0]=0; v2[0]=1; }
   }
    else
   {
  	if(s[i]=='R') { v1[i]=v1[i-1]+1; v2[i]=v2[i-1]; }
  	else 
  	{
  		v1[i]=v1[i-1]; 
  		v2[i]=v2[i-1]+1; 
  	}
   }
 }
 int ans=min(n-v2[n-1],n-v1[n-1]); int res=0; 
 for(int i=1;i<=n-2;i++)
 {  res=0; 
 	int nw=v2[i],nr=v1[n-1]-v1[i]; 
 	res=min(nw,nr); 
 	res+=abs(nw-nr); 
 	ans=min(ans,res); 
 }
 cout<<ans<<"\n"; return 0; 
}


Measured memory_bytes: 5200
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
	int y,m,d;
	cin>>y; getchar(); cin>>m; getchar(); cin>>d;
	if (y>2019)
	{
		printf("TBD");
		return 0;
	}
	if (y<2019)
	{
		printf("Heisei");
		return 0;
	}
	if (m>4)
	{
		printf("TBD");
		return 0;
	}
	if (m==4)
	{
		if (d>30)
		{
			printf("TBD");
			return 0;
		}
		printf("Heisei");
		return 0;
	}
	printf("Heisei");
	return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
struct graph{
  int N;
  int *es;
  int **edge;
  void setEdge(int N__, int M, int A[], int B[], void **mem = &wmem){
    int i;
    N = N__;
    walloc1d(&es, N, mem);
    walloc1d(&edge, N, mem);
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      es[A[i]]++;
      es[B[i]]++;
    }
    for(i=(0);i<(N);i++){
      walloc1d(&edge[i], es[i], mem);
    }
    for(i=(0);i<(N);i++){
      es[i] = 0;
    }
    for(i=(0);i<(M);i++){
      edge[A[i]][es[A[i]]++] = B[i];
      edge[B[i]][es[B[i]]++] = A[i];
    }
  }
  void getDist(int root, int res[], void *mem = wmem){
    int i;
    int j;
    int k;
    int*q;
    int s;
    int z;
    walloc1d(&q, N, &mem);
    for(i=(0);i<(N);i++){
      res[i]=-1;
    }
    res[root]=0;
    s=0;
    z=1;
    q[0]=root;
    while(z){
      i=q[s++];
      z--;
      for(j=(0);j<(es[i]);j++){
        k=edge[i][j];
        if(res[k]>=0){
          continue;
        }
        res[k]=res[i]+1;
        q[s+z++]=k;
      }
    }
  }
}
;
int N;
int U;
int V;
int A[100000];
int B[100000];
graph g;
int d1[100000];
int d2[100000];
int main(){
  int i;
  wmem = memarr;
  int res;
  rd(N);
  rd(U);U += (-1);
  rd(V);V += (-1);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N-1);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);A[Lj4PdHRW] += (-1);
      rd(B[Lj4PdHRW]);B[Lj4PdHRW] += (-1);
    }
  }
  g.setEdge(N,N-1,A,B);
  g.getDist(U,d1);
  g.getDist(V,d2);
  res = d1[V] / 2;
  for(i=(0);i<(N);i++){
    if(d1[i] < d2[i]){
      chmax(res, d2[i] - 1);
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20191214-1

// --- original code ---
// int N, U, V, A[1d5], B[1d5];
// graph g;
// int d1[1d5], d2[1d5];
// {
//   int res;
//   rd(N,U--,V--,(A--,B--)(N-1));
//   g.setEdge(N,N-1,A,B);
//   g.getDist(U,d1);
//   g.getDist(V,d2);
//   res = d1[V] / 2;
//   rep(i,N) if(d1[i] < d2[i]){
//     res >?= d2[i] - 1;
//   }
//   wt(res);
// }


Measured memory_bytes: 6400
numbers = ["0","1","2","3","4","5","6","7","8","9"]
A, B = map(int, input().split())
S = input()
f = True
for i in range(0,A):
  if S[i] not in numbers:
    f = False
for i in range(A+1,A+B+1):
  if S[i] not in numbers:
    f = False
if S[A] != "-":
  f = False
if f:
  print("Yes")
else:
  print("No")

Measured memory_bytes: 3064
import bisect
N = int(input())
A = [-int(input()) for _ in range(N)]
tmp = [A[0]]
for i in A[1:N]:
    index = bisect.bisect(tmp, i)
    if index == len(tmp):
        tmp.append(i)
    else:
        tmp[index] = i

print(len(tmp))


Measured memory_bytes: 8604
N = int(input())
A = list(map(int, input().split()))
L = [0] * (100001)
s = sum(A)
for a in A:
    L[a] += 1
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    L[C] += L[B]
    s += L[B] * (C-B)
    L[B] = 0
    print(s)


Measured memory_bytes: 20872
#include <bits/stdc++.h>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using boost::irange;
using boost::sort;
using boost::adaptors::reversed;

int main() {
    constexpr int MOD = 2019;
    string s;
    cin >> s;
    int n = s.length();
    
    int d = 1;
    vector<int> t(n);
    t[n - 1] = (s[n - 1] - '0') % MOD;

    for (int i : irange(n - 1) | reversed) {
        d = (d * 10) % MOD;
        t[i] = (t[i + 1] + d * (s[i] - '0')) % MOD;
    }

    vector<int> c(MOD);
    c[0] = 1;
    for (int m : t) c[m] += 1;

    int ans = 0;
    for (int m : c) ans += m * (m - 1) / 2;

    cout << ans << endl;
}


Measured memory_bytes: 4448
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define gadd(x,y) x=add(x,y)
#define gmul(x,y) x=mul(x,y)
using namespace std;

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

const int N = 1e6 + 6;
bool b[N] = {};

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	int n;
	cin >> n;
	vi a(n);
	rep(i,0,n)cin >> a[i];
	sort(all(a));
	int ans = 0;
	rep(i,0,n){
		if(b[a[i]] == 0){
			++ans;	
			if(i < n - 1 && a[i+1] == a[i]){
				--ans;	
			}
			for(int j = a[i];j < N; j += a[i]){
				b[j] = 1;
			}
		}
	}
	cout << ans << '\n';
}


Measured memory_bytes: 5024
var

  s : ansistring;
  i, cnt : longint;
  b : boolean;
  
begin

  readln(S);
  b := true;
  cnt := 0;
  
  for i := 1 to length(s)-1 do
  begin
    if s[i] <> s[i+1] then
      b := false;
  end;
  
  if b = true then
  begin
    writeln(0);
    halt;
  end
  else
    for i := 1 to length(s)-1 do
      if ((s[i] = 'B') and (s[i+1] = 'W')) or ((s[i] = 'W') and (s[i+1] = 'B')) then
        inc(cnt);
        
  writeln(cnt);
  

end.

Measured memory_bytes: 1376
A = [-1, 0, 0, 1]
B = [0, -1, 1, 0]


h, w = map(int, input().split())
GRID = ["." * (w + 2)] + ["." + input() + "." for _ in range(h)] + ["." * (w + 2)]

answer = "Yes"
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if GRID[i][j] == ".":
            continue
        flag = True
        for a, b in zip(A, B):
            if GRID[i + a][j + b] == "#":
                flag = False
        if flag:
            answer = "No"

print(answer)


Measured memory_bytes: 3064
package main

import "fmt"

func main() {
	var l, r int32

	fmt.Scan(&l)
	fmt.Scan(&r)

	if r >= l + 2019 {
		r = l + 2019
	}

	answer := int64(2018)
	for i := l; i < r; i++ {
		for j := i + 1; j <= r; j++ {
			rem := (int64(i) * int64(j)) % 2019
			if rem < answer {
				answer = rem
			}
		}
	}
	fmt.Println(answer)
}

Measured memory_bytes: 512
A,B,C,D = map(int,input().split())

if B<=C or A>=D:
  print(0)
elif A<=C and B>=C and D>=B:
  print(B-C)
elif A>=C and B<=D:
  print(B-A)
elif A<=C and B>=D:
  print(D-C)
elif A>=C and A<=D and B>=D:
  print(D-A)
  

Measured memory_bytes: 3060
/* -------------------------------- Template -------------------------------- */

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <locale>
#include <iostream>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()

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

template <typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }

// template<typename T> constexpr T inf = [](){ assert(false); };
// template<> constexpr int inf<int> = 1e9;
// template<> constexpr ll inf<ll> = 1e18;
// template<> constexpr ld inf<ld> = 1e30;

struct yes_no : std::numpunct<char> {
  string_type do_truename()  const { return "Yes"; }
  string_type do_falsename() const { return "No"; }
};

void solve();

int main() {
  std::locale loc(std::locale(), new yes_no);
  std::cout << std::boolalpha << std::setprecision(12) << std::fixed;
  std::cout.imbue(loc);
  solve();
  return 0;
}

using namespace std;

/* -------------------------------- Library -------------------------------- */

/* ---------------------------------- Main ---------------------------------- */

void solve() {
  int W, H, N;
  cin >> W >> H >> N;
  int lx = 0, rx = W, ly = 0, ry = H;
  REP(i,N) {
    int x, y, a;
    cin >> x >> y >> a;
    if (a == 1) chmax(lx, x);
    if (a == 2) chmin(rx, x);
    if (a == 3) chmax(ly, y);
    if (a == 4) chmin(ry, y);
  }
  cout << max(rx - lx, 0) * max(ry - ly, 0) << endl;
  return;
}


Measured memory_bytes: 256
// 蟻本: 例題 2-1-1　部分和問題 の類題
/*
 与えられたのが 1234 のときは
 1234,
 1 +234, 1 +2 +34, 1 +2 +3 +4,
 12 +34, 12 +3 +4,
 123 +4
 の合計が聞かれてる
 ↑を
 [1] 234, [1, 2] 34, [1, 2, 3] 4
 [12] 34, [12, 3] 4,
 [123] 4
 のように+で区切り終えたものを配列で管理し,
 まだ+で区切る余地のあるものを文字列で扱いました(ノ)>◡<(ヾ)ﾓﾁﾓﾁ
 */

func dfs(_ s: String) {
    if s.count==0 { return }    // ベースケース(・・？) (文字列が空("")なら終了)
    
    for i in 1..<s.count {
        let idx: String.Index = s.index(s.startIndex, offsetBy: i)  // +で区切る場所を指定
        seq.append(Int(s[..<idx])!) // +で区切り終わった部分を記憶
        res += seq.reduce(0, +) + Int(s[idx...])!   // 計算, .reduce(0, +)で配列の和を計算できる
        
        
        dfs(String(s[idx...]))  // 再帰呼び出し
        seq.removeLast()
    }
}

let S: String = readLine()!
var seq: [Int] = []
var res = Int(S)!

dfs(S)  // 再帰スタート٩(๑❛ᴗ❛๑)۶
print(res)  // (๑•ૅㅁ•๑)

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

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> h(N);
    rep(i, N) cin >> h[i];

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

    int res = 1000000001;
    rep(i, N-K+1) {
        int temp = h[i+K-1] - h[i];
        //cout << temp << endl;
        res = min(res, temp);
    }

    cout << res << endl;
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
  int n;
  cin>>n;
  int y=n%100;
  int x=n/100;
  if(x==0&&1<=y&&y<=12){
    cout<<"YYMM"<<endl;
  }
  else if(0<x&&x<13&&y==0){
    cout<<"MMYY"<<endl;
  }
  else if(1<=x&&x<=12&y>=13){
    cout<<"MMYY"<<endl;
  }
  else if(x>=13&&1<=y&&y<=12){
    cout<<"YYMM"<<endl;
  }
  else if(1<=x&&x<=12&&1<=y&&y<=12){
    cout<<"AMBIGUOUS"<<endl;
  }
  else cout<<"NA"<<endl;
}

Measured memory_bytes: 3608
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int dis[200005];

int main()
{
	int k, n;
	cin >> k >> n;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &dis[i]);
	}
	sort(dis, dis + n);
	int MAX = 0;
	for (int i = 1; i < n; i++)
	{
		MAX = max(MAX, dis[i] - dis[i - 1]);
	}
	MAX = max(MAX, dis[0] + k - dis[n - 1]);
	cout << k - MAX << endl;
	return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define int long long

template<class T> istream& operator>>(istream &is,vector<T> &st){
  for(size_t i=0;i<st.size();++i) is >> st[i];
  return is;
}
template<class T> ostream& operator<<(ostream &os, const vector<T> &st){
  for(size_t i=0;i<st.size();++i){
    if(i==st.size()-1) os << st[i];
    else os << st[i] << " ";
  }
  return os;
}

constexpr int MOD = 1e9+7;
template<int mod>
struct ModInt {
  int x;
 
  constexpr ModInt() : x(0) {}
  constexpr ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  constexpr ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  constexpr ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  constexpr ModInt operator-() const { return ModInt(-x); }
  constexpr ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  constexpr ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  constexpr ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  constexpr ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  constexpr bool operator==(const ModInt &p) const { return x == p.x; }
  constexpr bool operator!=(const ModInt &p) const { return x != p.x; }
  constexpr ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t = 0;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  constexpr ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
 
using modint = ModInt<MOD>;
 

vector<pair<int, int>> prime_factorization(int n) {
    vector<pair<int, int>> ret;
    int i = 2;
    for (; i * i <= n; i++) {
        int cnt = 0;
        while (n % i == 0) ++cnt, n /= i;
        if (cnt) ret.push_back({i, cnt});
    }
    ret.push_back({n, 1});
    return ret;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int N;
    cin >> N;
    vector<int> A(N);
    cin >> A;
    map<int, int> m;
    for (int a : A) {
        for (auto& p: prime_factorization(a)) {
            m[p.first] = max(p.second, m[p.first]);
        }
    }
    modint lcm(1);
    for (auto& p: m) {
        lcm *= modint(p.first).pow(p.second);
    }
    modint ans(0);
    for(int i = 0; i < N; i++) {
        ans += lcm / modint(A[i]);
    }
    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 1024
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def conv(x, n):
    if x < 0:
        return None
    if n < 2 or 16 < n:
        return None

    if x == 0:
        return 0

    nchar = '0123456789ABCDEF'
    result = ''

    while x > 0:
        result = nchar[x % n] + result
        x = x // n

    return result


def main():
    N, K = MI()
    print(len(conv(N, K)))


if __name__ == '__main__':
    main()


Measured memory_bytes: 40304
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
#define all(x) begin(x),end(x)
#define sz(x) ll((x).size())
#define TR(X) ({ if(1) cerr << "Trace: " << (#X) << " = " << (X) << endl; })

typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	const ll P = 998244353LL;

	vector<ll> twos(5*1e5); twos[0] = 1LL;
	for(ll i = 1; i < sz(twos); i++) (twos[i] = twos[i - 1] * 2) %= P;

	ll n; cin >> n;
	vector<pair<ll, ll> > p(n);
	for(ll i = 0; i < n; i++) {
		ll x,y; cin >> x >> y;
		p[i] = make_pair(x,y);
	}

	sort(all(p));
	for(ll i = 0; i < n; i++) {
		swap(p[i].first, p[i].second);
		p[i].second = i;
	}
	sort(all(p));
	for(ll i = 0; i < n; i++) {
		swap(p[i].first, p[i].second);
		p[i].second = i;
	}
	sort(all(p));

	ll res = 0;
	ordered_set X;
	for(ll i = 0; i < n; i++) {
		X.insert(p[i].second);
		ll lb = X.order_of_key(p[i].second);
		ll l = p[i].second, b = p[i].first;
		ll r = n - l - 1, u = n - b - 1;
		ll rb = b - lb, ru = r - rb, lu = l - lb;
		assert(lu == u - ru);
		ll cur = 0;
		(cur += twos[l]) %= P;
		(cur += twos[r]) %= P;
		(cur += twos[b]) %= P;
		(cur += twos[u]) %= P;
		((cur -= twos[lu]) += P) %= P;
		((cur -= twos[ru]) += P) %= P;
		((cur -= twos[rb]) += P) %= P;
		((cur -= twos[lb]) += P) %= P;
		(res += twos[n] - cur - 1) %= P;
	}
	cout << (res + P) % P << endl;
}


Measured memory_bytes: 19840
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<deque>
#include<set>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
typedef pair<int,int> P;

const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

int main(){
    ll n,m;
    cin >> n >> m;
    vector<vector<ll> > s(m);
    vector<ll> k(m);
    vector<ll> p(m);
    for(int i=0;i<m;i++){
        cin >> k[i];
        for(int j=0;j<k[i];j++){
            ll a;
            cin >> a; a--;
            s[i].push_back(a);
        }
    }
    rep(i,m) cin >> p[i];

    ll ans=0;
    for(ll bit=0;bit<(1<<n);bit++){
        vector<ll> cnt(n,0);
        for(int j=0;j<n;j++){
            if(bit & (1<<j)){
                cnt[j]++;
            }
        }

        vector<ll> count(m,0);
        for(int a=0;a<m;a++){
            for(int b=0;b<k[a];b++){
                count[a] += cnt[s[a][b]];
            }
        }

        bool flag=true;
        for(int x=0;x<m;x++){
            if(count[x]%2==p[x]){
                flag = true;
            }else{
                flag = false; break;
            }
        }
        if(flag) ans++;
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
//deque/functional/iterator/list/map/memory/numeric/queue/set/stack/utility/vector
//#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	ll s=0;
	for(int i=1;i<=n-1;i++){
		s+=i;
	}
	cout<<s<<endl;
return 0;
}


Measured memory_bytes: 764
#include <bits/stdc++.h>
#define int long long
#define rng(i, l, r) for (size_t i = (l); i < (r); ++i)
#define rep(i, n) rng(i, 0, n)
#define gnr(i, l, r) for (size_t i = (r)-1; i >= (l); i--)
#define per(i, b) gnr(i, 0, b)
#define ALL(obj) (obj).begin(), (obj).end()    //1,2,3,...
#define rALL(obj) (obj).rbegin(), (obj).rend() //...,3,2,1

using namespace std;

const int INF = 1e18;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

void solve()
{

    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

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

    int n;
    cin >> n;
    cout << (1 + n - 1) * (n - 1) / 2 << endl;
}

signed main()
{
    solve();
    return 0;
}


Measured memory_bytes: 256
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on
 
template<typename InputIterator> constexpr std::string join(InputIterator first, InputIterator last, const std::string &sep) {
  if (first == last) return "";
  std::stringstream ss;
  ss << *(first++);
  for_each(first, last, [&](auto &x) { ss << sep << x; });
  return ss.str();
}
 
vpii f(int h, int w) {
  vvi visited(h, vi(w));
  vi di = {0, 1, 0, -1}, dj = {1, 0, -1, 0};
  int t = 0;
  vpii ans(h * w);
  queue<tuple<int, int, int>> q;
  q.emplace(0, 0, 0);
  while (q.size()) {
    int i, j, d;
    tie(i, j, d) = q.front();
    q.pop();
    ans[d] = {i, j};
    visited[i][j] = true;
    if (i + di[t] < 0 || j + dj[t] < 0 || i + di[t] >= h || j + dj[t] >= w || visited[i + di[t]][j + dj[t]]) t = (t + 1) % 4;
    i += di[t], j += dj[t];
    if (i < 0 || j < 0 || i >= h || j >= w || visited[i][j]) continue;
    q.emplace(i, j, d + 1);
  }
  return ans;
}
 
main {
  int h, w, n;
  cin >> h >> w >> n;
  vi a(n);
  cin >> a;
  vpii table = f(h, w);
  vvi ans(h, vi(w));
  int c = 0;
  rep(k, n) {
    while (a[k]--) {
      int i, j;
      tie(i, j) = table[c];
      ans[i][j] = k + 1;
      c++;
    }
  }
  for (auto &x : ans) cout << join(all(x), " ") << endl;
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n)              for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

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

	int n; cin >> n;
	vector<int> as(n); for (auto& a : as) { cin >> a; }
	vector<int> bs(n); for (auto& b : bs) { cin >> b; }


	unordered_map<int, int> amap;
	vector<int> acs(200001);
	FOR_LT(i, 0, n) {
		int a = as[i];
		if (amap.find(a) == amap.end()) {
			amap.insert({ a, i });
		}
		acs[a]++;
	}
	vector<int> bcs(200001);
	for (auto& b : bs) {
		bcs[b]++;
	}

	multimap<int, int> cmap;
	FOR_LE(i, 1, 200000) {
		if (n < acs[i] + bcs[i]) {
			cout << "No" << endl;
			return 0;
		}
		if (acs[i] != 0) {
			cmap.insert({ acs[i] + bcs[i], i });
		}
	}
	cout << "Yes" << endl;

	set<int> ps;
	FOR_LT(i, 0, n) {
		ps.insert(i);
	}

	vector<int> ans(n);
	auto cit = cmap.rbegin();
	while (cit != cmap.rend()) {
		int a = cit->second;
		//cout << celem.first << " " << celem.second << endl;
		//cout << p << " " << a << " " << acs[a] << endl;
		int an = acs[a];
		int ansp = amap[a] + an;
		FOR_LT(i, 0, bcs[a]) {
			auto it = ps.lower_bound(ansp);
			if (it == ps.end()) {
				it = ps.begin();
			}
			ansp = *it;
			ps.erase(it);
			ans[ansp] = a;
		}
		bcs[a] = 0;
		cit++;
	}

	int ansp = 0;
	FOR_LE(i, 1, 200000) {
		if (bcs[i] != 0) {
			FOR_LT(j, 0, bcs[i]) {
				auto it = ps.lower_bound(ansp);
				ansp = *it;
				ps.erase(it);
				ans[ansp] = i;
			}
		}
	}

	FOR_LT(i, 0, n) {
		cout << ans[i] << " ";
	}
	cout << endl;
	

	return 0;
}

Measured memory_bytes: 33980
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
ll MOD_N = 1000000007;

ll calc_p(int n, int k) {
    ll ans = 1;
    for (int i = 0; i < k; i++) {
        ans *= (n - i);
        ans %= MOD_N;
    }
    return ans;
}

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> es[N];
    for (int i = 0; i < N-1; ++i) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        es[a].push_back(b);
        es[b].push_back(a);
    }
    bool visited[N];
    for (int i = 0; i < N; ++i) {
        visited[i] = false;
    }
    ll c = 1;
    stack<int> nodes_to_visit;
    nodes_to_visit.push(0);
    while (!nodes_to_visit.empty()) {
        int n = nodes_to_visit.top();
        nodes_to_visit.pop();
        visited[n] = true;
        int num_children = es[n].size();
        for (int i = 0; i < num_children; ++i) {
            int m = es[n][i];
            if (!visited[m]) {
                nodes_to_visit.push(m);
            }
        }
        if (n == 0) {
            c *= calc_p(K, num_children + 1);
        } else {
            if (num_children > 1) {
                c *= calc_p(K - 2, num_children - 1);
            }
        }
        c %= MOD_N;
    }
    cout << c << "\n";
    return 0;
}


Measured memory_bytes: 6648
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp111 {
    class Program {
        static int mod = 1000000007;
        static void Main(string[] args) {
            int N = int.Parse(Console.ReadLine());
            var A = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var ordered_A = A.OrderBy(x => x).ToArray();

            var idealList = new List<int>();

            if (N % 2 == 1) {
                long k = (N - 1) / 2;
                idealList.Add(0);
                for (int i = 1; i <= k; i++) {
                    idealList.Add(i * 2);
                    idealList.Add(i * 2);
                }
                if (!ordered_A.SequenceEqual(idealList.ToArray())) {
                    Console.WriteLine(0);
                } else {
                    long ans = 1;
                    for (int i = 1; i <= k; i++) {
                        ans = ans * 2 % mod;
                    }
                    Console.WriteLine(ans);
                }
            } else {
                long k = N / 2;
                for (int i = 0; i < k; i++) {
                    idealList.Add(i * 2 + 1);
                    idealList.Add(i * 2 + 1);
                }
                if (!ordered_A.SequenceEqual(idealList.ToArray())) {
                    Console.WriteLine(0);
                } else {
                    long ans = 1;
                    for (int i = 1; i <= k; i++) {
                        ans = ans * 2 % mod;
                    }
                    Console.WriteLine(ans);
                    //Console.WriteLine(Expo_2_Mod(k));
                }
            }
        }

        static long Expo_2_Mod(long a) {
            if (a == 1) {
                return 2;
            }
            return 2 * Expo_2_Mod(a - 1) % mod;
        }

        static bool CheckList(List<int> a , List<int> b) {
            int cnt = a.Count();

            for (int i = 0; i < cnt; i++) {
                if (a[i] != b[i]) {
                    return false;
                }
            }
            return true;
        }
    }
}


Measured memory_bytes: 24416
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
using namespace std;
#define REP(i, limit) for(int i=0; i< limit; ++i)
#define FOR(i, j, limit) for(int i=j; i< limit; ++i)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ECHO(x) cout<<x<<endl;
#define LL long long
int INF = 2147483647;
LL LINF = 9223372036854775807;
LL MOD = 1000000007;
typedef pair<int, int>P;
typedef pair<LL, LL>PL;
typedef vector<int> vi;
typedef vector<LL> vl;



int main(){
  int a[3], b[3];
  REP(i, 3){
    cin>>a[i]>>b[i];
  }
  int c[4];
  REP(i, 4)c[i]=0;
  REP(i, 3){
    ++c[a[i]-1];
    ++c[b[i]-1];
  }
  sort(c, c+4);
  bool ans=true;
  if(c[0]!=1)ans=false;
  if(c[1]!=1)ans=false;
  if(c[2]!=2)ans=false;
  if(c[3]!=2)ans=false;
  if(ans)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
	
		Scanner in = new Scanner(System.in);
		
		int l=in.nextInt(),r=in.nextInt(),d=in.nextInt();
		int count = 0;
		
		for(int i = l;i<=r;i++) {
			if( i % d == 0) {
				count++;
			}
		}
		
		System.out.println(count);

	}

}


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

int main(){
	int K, S;
	cin >> K >> S;

	int ans = 0;
	for (int X = 0; X <= K&&X<=S; ++X){
		for (int Y = 0; Y + X <= S && Y<=K; ++Y){
			if (S - (X + Y) <= K){
				++ans;
			}
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 384
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int main()
{
    int w, h, t, p;
    cin >> w >> h >> t >> p;

    vector<int> x(p), y(p);
    for(int i=0; i<p; ++i){
        int tmp;
        cin >> x[i] >> y[i] >> tmp;
    }

    vector<vector<int> > s(h, vector<int>(w));
    for(int i=0; i<h; ++i){
        for(int j=0; j<w; ++j){
            cin >> s[i][j];
        }
    }

    int ans = 0;
    for(int i=0; i<p; ++i){
        if(s[y[i]][x[i]] == 1)
            ++ ans;
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3156
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define mp make_pair
using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll INF=1LL<<30;
const ll LINF=1LL<<61;
const double eps=1e-5;
const ll MOD=1e9+7;
int main(){
    int n,m;cin>>n>>m;
    vector<vector<int>> G(n);
    vector<int> in(n);
    for(int i=0;i<m;i++){
        int u,v;cin>>u>>v;--u;--v;
        G[u].push_back(v);
        in[v]++;
    }
    stack<int> st;
    for(int i=0;i<n;i++){
        if(in[i]==0)st.push(i);
    }
    vector<int> vs;
    while(!st.empty()){
        int v=st.top();st.pop();
        vs.push_back(v);
        for(auto e:G[v]){
            in[e]--;
            if(in[e]==0)st.push(e);
        }
    }
    int ans=0;
    vector<int> dp(n+10);
    for(int i=0;i<vs.size();i++){
        ans=max(ans,dp[vs[i]]);
        for(auto e:G[vs[i]]){
            dp[e]=max(dp[e],dp[vs[i]]+1);
        }
    }
    cout<<ans<<endl;
}   


Measured memory_bytes: 7032
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<complex>
#include<functional>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<stack>
#include<set>

using namespace std;
#define endl '\n'
#define pb push_back
#define fst first
#define scd second
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define REP(i,n) for(int i = 0;i < (n);i++)
#define FOR(i,a,b) for(int i = (a);i <= (b);i++)
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl

constexpr int MOD = 1000000007;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;

template <class T = int> T in() { T x; cin >> x; return (x); }
template <class T = int> void out(T x) { cout << (x) << endl; }

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

// value
ll L;
int N;
vl X;
vl sumX;

void solve() {

	cin >> L >> N;
	for (int i = 0; i < N; i++) {
		ll tmp;
		cin >> tmp;
		X.emplace_back(tmp);
		if (i > 0) sumX.emplace_back(X[i] + sumX[i - 1]);
		else sumX.emplace_back(X[i]);
	}

	ll ans = 0LL;

	for (int i = 0; i < N; i++) {
		ll tmp;
		int c = (N - i) / 2;
		if ((N - i) % 2 == 0) {
			tmp = 2 * ((i + c - 1 >= 0 ? sumX[i + c - 1] : 0) - (i ? sumX[i - 1] : 0)) + 2 * (c - 1) * L - 2 * (sumX[N - 1] - sumX[i + c]) + (L - X[i + c]);
		}
		else {
			tmp = 2 * ((i + c - 1 >= 0 ? sumX[i + c - 1] : 0) - (i ? sumX[i - 1] : 0)) + 2 * c * L - 2 * (sumX[N - 1] - sumX[i + c]) + X[i + c];
		}
		ans = max(ans, tmp);
		if ((N - i) % 2 == 0) {
			tmp = 2 * (c >= 2 ? sumX[c - 2] : 0) + 2 * c * L - 2 * (sumX[N - 1 - i] - sumX[N - 1 - i - c]) + X[N - 1 - i - c];
		}
		else {
			tmp = 2 * (c >= 1 ? sumX[c - 1] : 0) + 2 * c * L - 2 * (sumX[N - 1 - i] - sumX[c]) + (L - X[c]);
		}
		ans = max(ans, tmp);
	}

	cout << ans << endl;

	return;

}

int main() {

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

	solve();

	return 0;

}


Measured memory_bytes: 3564
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
typedef long long ll;
using namespace std;
const ll mod=1000000007,mod9=1000000009;
template <class T>inline T lcm(T a,T b){return (a*b/__gcd(a,b));}

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

    int n=s.length();
    for(int i=0;i<n;i++){
        if(i%2==0){
            if(s[i]=='L'){
                cout << "No"<<endl;
                return 0;
            }
        }
        else{
            if(s[i]=='R'){
                cout << "No"<<endl;
                return 0;
            }
        }
    }
    cout << "Yes"<<endl;
    return 0;
}



Measured memory_bytes: 256
import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val a = sc.nextInt()
    val b = sc.nextInt()

    println(problem137_a(a, b))
}

fun problem137_a(a: Int, b: Int): Int {

    val plus = a + b
    val minus = a - b
    val kake = a * b

    return intArrayOf(plus, minus, kake).max()!!
}

Measured memory_bytes: 36036
n, h = [int(t) for t in input().split()]
aa = []
bb = []
for _ in range(n):
    a, b = [int(t) for t in input().split()]
    aa.append(a)
    if b > a:
        bb.append(b)
aa.sort()
bb.sort()

cnt = 0
while h > 0:
    if not bb or aa[-1] >= bb[-1]:
        cnt += (h - 1) // aa[-1] + 1
        break
    else:
        h -= bb.pop()
    cnt += 1
print(cnt)


Measured memory_bytes: 11352
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;
using Console = System.Console;

namespace Atcoder
{
    class Program
    {
        private const long COMDIV = 1000000007;

        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var S = Console.ReadLine();
            var array = new List<int>();
            int count = S.Skip(1).Count(x => x == 'E');
            array.Add(count);
            for (int i = 1; i < N; i++)
            {
                if (S[i] == 'E') --count;
                if (S[i - 1] == 'W') ++count;
                array.Add(count);
            }

            Console.WriteLine(array.Min());
        }
    }
}

Measured memory_bytes: 17504
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep4(i, a, b, c) for (int i = int(a), i##__b = int(b), i##__c = int(c); i < i##__b; i += i##__c)
#define rep1(n) rep4(i, 0, n, 1)
#define rep2(i, n) rep4(i, 0, n, 1)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define REP(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep3(i, a, b) for (int i = int(b) - 1, i##__a = int(a); i >= i##__a; i--)
#define rrep1(n) rrep3(i, 0, n)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep4(i, a, b, c) for (int i = int(a + (b - a - 1) / c * c), i##__a = int(a), i##__c = int(c); i >= i##__a; i -= i##__c)
#define RREP(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define FOREACH(i, x) for (auto &i : x)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define mt make_tuple
#define print(x) cout << x << endl;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vbool = vector<bool>;
using vvbool = vector<vector<bool>>;
using vstring = vector<string>;
template <class T> using vv = vector<vector<T>>;
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;}

struct Point {int x, y; Point(int x, int y):x(x), y(y){}};
int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1};
pii dvs[] = {mp(1, 0), mp(0, 1), mp(-1, 0), mp(0, -1)};
// clang-format on


bool isATGC(char c) {
    return c == 'A' || c == 'C' || c == 'G' || c == 'T';
}


int main() {
    string S;
    cin >> S;
    int ans = 0;
    int now = 0;
    FOREACH(c, S) {
        if (isATGC(c)) {
            now++;
        } else {
            chmax(ans, now);
            now = 0;
        }
    }
    print(max(ans, now));
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10000)
dp = []
coins = []
INF = 9999999

def change(n, m):
    if n == 0:
        return 0
    if dp[n] == INF:
        res = INF
        for j in range(0, m):
            if n - coins[j] >= 0:
                res = min(res, change(n - coins[j], m) + 1)
        dp[n] = res
    return dp[n]

line = input()
n, m = list(map(int, line.split()))
line = input()
d = list(map(int, line.split()))
coins = sorted(d)
coins.reverse()
dp = [INF] * (n + 1)
change(n, m)
print(dp[n])


Measured memory_bytes: 8624
a = input()
b = input()

gt = 'GREATER'
lt = 'LESS'
eq = 'EQUAL'

if len(a) > len(b):
    print(gt)
elif len(a) < len(b):
    print(lt)
elif a == b:
    print(eq)
else:
    for ca, cb in zip(a, b):
        if ca == cb:
            continue
        if ca > cb:
            print(gt)
        else:
            print(lt)
        exit(0)


Measured memory_bytes: 9104
#include <bits/stdc++.h>
#define pi 3.14159
#define INF 10000000
using namespace std;
int main(){
	int n;
	cin >> n;
	int a[n],b[n];
	for(int i=0;i<n;i++){
		cin >> a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	for(int i=0;i<n;i++){
		if(a[i]>=b[n/2])
			cout << b[n/2-1] << endl;
		else
			cout << b[n/2] << endl;
	}
}

Measured memory_bytes: 3712
#include<bits/stdc++.h>
using namespace std;
int dp[100001][3];
int main()
{
  int n,i,pre,a[100001],b[100001],c[100001];
  cin>>n;
  for(i=0;i<n;i++)
  {
      cin>>a[i]>>b[i]>>c[i];
  }
  for(i=0;i<n;i++)
  {
     for(int j=0;j<3;j++)
     {
         if(i==0)
         {
        if(j==0)
            dp[i][j]=a[i];
        else if(j==1)
            dp[i][j]=b[i];
        else
            dp[i][j]=c[i];
         }
         else
         {
        for(int k=0;k<3;k++)
        {
        if(j==k)
            continue;
        else
        {
         if(j==0)
          dp[i][j]=max(dp[i][j],dp[i-1][k]+a[i]);
          else if(j==1)
            dp[i][j]=max(dp[i][j],dp[i-1][k]+b[i]);
           else
         dp[i][j]=max(dp[i][j],dp[i-1][k]+c[i]);
        }}}}}
  cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));

}


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

int main(){
  int N, K, s;
  s = 1;
  cin >> N >> K;
  
  for(int i=0; i<N; i++){
    if(s < K){
      s = s * 2;
    }else{
      s = s + K;
    }
  }
  cout << s << endl;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using ll = long long;
#define vc vector
using namespace std;
int main() {
  string S;
  cin>>S;
  int a[26]={0};
  for (int i(0);i<S.size();i++){
    a[S[i]-'a']+=1;
  }
  for (int i(0);i<26;i++){
    if (a[i]!=1&&a[i]!=0){
      cout << "no"<<endl;
      return 0;
    }
  }
  cout << "yes"<<endl;
  return 0;
}



Measured memory_bytes: 256