Created
January 4, 2019 15:20
-
-
Save nukopy/e7631fa50f799f595c91e5330adfbb25 to your computer and use it in GitHub Desktop.
ABC014: B - 価格の合計
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <string> | |
#include <vector> | |
#include <iterator> // std::back_inserter() | |
#include <set> | |
#include <map> | |
#include <algorithm> // std::copy() | |
#include <functional> // std::greater<T>() | |
#include <utility> // std::swap() | |
#include <numeric> // accumulate(ALL(vec), 0) 0 は初期値 | |
#include <cmath> | |
#include <climits> // INT_MIN | |
#include <cctype> // std::isdigit() | |
#include <iomanip> // std::setprecision() | |
#include <bitset> | |
#include <sstream> | |
using namespace std; | |
#define ALL(obj) (obj).begin(), (obj).end() | |
#define REP(i, n) for (int i=0; i < (int)(n); i++) // 0 ~ n-1 | |
#define REPN(i, n) for (int i=1; i <= (int)(n); i++) // 1 ~ n | |
#define MIN(vec) min_element(ALL((vec))) // イテレータのため、値を取り出すときは * を先頭につける | |
#define MAX(vec) max_element(ALL((vec))) | |
#define IDX(vec, element_iter) distance((vec).begin(), element_iter) | |
#define SUM(vec) accumulate(ALL((vec)), 0) // 0 は初期値 | |
#define COPY(vec1, vec2) copy(ALL(vec1), back_inserter(vec2)) // vec1をvec2にコピーする vec2は空にしておく必要あり | |
typedef long long ll; | |
const int MOD = 1000000007; // 1 000 000 007 | |
int main() { | |
cin.tie(0); | |
ios::sync_with_stdio(false); | |
// input | |
int N, X; | |
cin >> N >> X; | |
vector<int> vec(N); | |
REP(i, N) cin >> vec[i]; | |
// calculation | |
stringstream ss; | |
const int num = 20; | |
ss << std::bitset<num>(X); | |
string binary_str = ss.str(); | |
string sub = binary_str.substr(num-N, num); | |
reverse(sub.begin(), sub.end()); | |
int len = (int)sub.length(); | |
int sum = 0; | |
for (int j = 0; j < len; ++j) { | |
if (sub[j] == '1') { | |
sum += vec[j]; | |
} | |
} | |
cout << sum << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment