1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AtCoder】ABC074- Collectiong Balls

Last updated at Posted at 2024-03-05

学習記録用 -B- Collectiong Balls ABC074

// 2 N
// 9 K
// 3 6 x1,x2...

// 12
#include <bits/stdc++.h>
using namespace std;

int main () {
 int N,K,X;
 cin >> N;
 cin >> K;
 int sum = 0;
 for (int i = 0; i < N; i++){
    cin >> X;
    int A = abs(X - 0);//(絶対値つけたい)
    int B = abs(X - K);//(絶対値つけたい)
    if(A<=B){
      sum += A*2;//(+=でないと、forループ更新のたびにsumが蓄積されない)
    }else{
      sum += B*2;
    }
 }
 cout << sum << endl;
 
}

sum = A2 / sum = B2を最初していて、時間がかかりました!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?