Language: C++

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

//combination(n,k)%p
const int cmax = 1048576;
const int mod = 1000000007;
vector<int64_t> fac(cmax), finv(cmax), inv(cmax);
void cominit(){
  fac.at(0) = fac.at(1) = 1;
  finv.at(0) = finv.at(1) = 1;
  inv.at(1) = 1;
  for(int i = 2; i < cmax; i++){
    fac.at(i) = fac.at(i-1)*i%mod;
    inv.at(i) = mod - inv.at(mod%i) *(mod/i)%mod;
    finv.at(i) = finv.at(i-1)*inv.at(i)%mod;
  }
}
int64_t commod(int n, int k){
  if(n < k) return 0;
  if(n < 0 || k < 0) return 0;
  return fac.at(n)*(finv.at(k)*finv.at(n-k)%mod)%mod;
}
//combination(n,k)
int64_t com(int n, int k){
  if(n < k) return 0;
  if(n < 0 || k < 0) return 0;
  return com(n-1,k-1)+com(n-1,k);
}
//1,3,7,9:true; 2,4,5,6,8:false;
bool parity(int n){
  if(n*n*n*n%10 == 1)
    return 1;
  else
    return 0;
}
//x^y
int64_t power(int64_t x, int y){
  if(y < 0)
    return 0;
  else{
    int64_t n = 1ll;
    for(int i = 0; i < y; i++)
      n *= x;
    return n;
  }
}
//x^y%p
int powermod(int x, int y, int p){
  if(y < 0)
    return 0;
  else{
    int n = 1;
    x %= p;
    for(int i = 0; i < y; i++){
      n = n*x%p;
    }
    return n;
  }
}
//xのn^y位の数
int digit(int64_t x, int y, int n){
  return x%power(n,y+1)/power(n,y);
}
//nの絵札枚数
int card(int64_t n){
  int cnt = 0;
  while(n > 0){
    int64_t n0 = n%10;
    n /= 10;
    if(n0 <= 3 && n%10 == 1){
      cnt++;
      n /= 10;
    }
  }
  return cnt;
}
int64_t card_comb(int64_t p){
  int64_t cmb = 0;
  while(p > 0){
    cmb += power(10,p%10);
    p /= 10;
  }
  return cmb;
}
//vecのk以上の要素のうち一番左のもの
int left(vector<int> vec, int k){
  int a = 0;
  int b = vec.size()-1;
  if(vec.at(a) > k)
    return 0;
  else if(vec.at(a) == k)
    return a;
  else if(vec.at(b) < k)
    return -1;
  else{
    bool bk = true;
    while(bk){
      int c = (a+b)/2;
      if(vec.at(c-1) < k && vec.at(c) == k){
        return c;
        bk = false;
      }
      else if(vec.at(c-1) >= k)
        b = c;
      else if(vec.at(c) < k)
        a = c;
      else{
        return c;
        bk = false;
      }
    }
  }
}
//vecのk以下の要素のうち一番右のもの
int right(vector<int> vec, int k){
  int a = 0;
  int b = vec.size()-1;
  if(vec.at(a) > k)
    return -1;
  else if(vec.at(b) == k)
    return b;
  else if(vec.at(b) < k)
    return b;
  else{
    bool bk = true;
    while(bk){
      int c = (a+b)/2;
      if(vec.at(c) == k && vec.at(c+1) > k){
        return c;
        bk = false;
      }
      else if(vec.at(c+1) <= k)
        a = c;
      else if(vec.at(c) > k)
        b = c;
      else{
        return c;
        bk = false;
      }
    }
  }
}
long long bnps(int n, int a, int d){
  long long an = 0;
  for(int i = 0; i < d; i++){
    an += a%power(n,i+1)/power(n,i)*power(10,i);
  }
  return an;
}
bool pt(long long n,vector<long long>vec){
  long long s = vec.size();
  for(int i = 0; i < s; i++){
    if(n%vec.at(i) == 0)
      return 0;
    else if(vec.at(i+1)*vec.at(i+1) > n)
      return 1;
  }
  return 0;
}
int64_t pmax = 134217728;
vector<int64_t> ntrl(pmax);
vector<int64_t> prime ={2,3};
void vprime(){
  for(long long i = 0; i < pmax; i++){
    if(i <= 1)
      ntrl.at(i) = -1;
    else
      ntrl.at(i) = i;
  }
  int64_t imax = sqrt(pmax);
  for(int64_t i = 0; i < imax; i++){
    int64_t p = prime.at(i);
    int64_t q = prime.at(i+1);
    for(int64_t j = p*p; j < pmax; j += p)
      ntrl.at(j) = -1;
    for(int64_t k = p*p+1; k < q*q && k < pmax; k++)
      if(ntrl.at(k) > 0)
        prime.push_back(k);
  }
}
int main() {
  int n;
  cin >> n;
  vector<int>w(n);
  rep(i,n)  cin >> w.at(i);
  sort(w.begin(),w.end());
  int sum = 0;
  rep(i,n) sum += w.at(i);
  int ans = sum;
  vector<int>v(w.at(w.size()-1));
  for(int x:w)  v.at(x-1) +=  x;
  int sumk = 0;
  for(int y : v){
      if(abs(sum-sumk*2) < abs(sum-2*(sumk+y)){
          cout << abs(sum-sumk*2) << endl;
          break;
      }
      else sumk += y;
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:195:47: error: expected ';' before '{' token
  195 |       if(abs(sum-sumk*2) < abs(sum-2*(sumk+y)){
      |                                               ^
      |                                               ;
a.cc:199:7: error: expected primary-expression before 'else'
  199 |       else sumk += y;
      |       ^~~~
a.cc:198:8: error: expected ')' before 'else'
  198 |       }
      |        ^
      |        )
  199 |       else sumk += y;
      |       ~~~~
a.cc:195:9: note: to match this '('
  195 |       if(abs(sum-sumk*2) < abs(sum-2*(sumk+y)){
      |         ^
a.cc: In function 'int left(std::vector<int>, int)':
a.cc:114:1: warning: control reaches end of non-void function [-Wreturn-type]
  114 | }
      | ^
a.cc: In function 'int right(std::vector<int>, int)':
a.cc:143:1: warning: control reaches end of non-void function [-Wreturn-type]
  143 | }
      | ^