1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

string firstLine, scndLine;
vector<int> top, bottom,b;

string toBinaryString( unsigned long n )
  {
  char     result[ (sizeof( unsigned long ) * 8) + 1 ];
  unsigned index  = sizeof( unsigned long ) * 8;
  result[ index ] = '\0';

  do result[ --index ] = '0' + (n & 1);
  while (n >>= 1);

  return string( result + index );
  }

void getChainB(){
    for(int i=0; i<top[0]; i++){
    b.push_back(top[1]-i);
    }
}

int numberOfOnes(string s){
    int i=0;
    for( int k=0; k<s.size();k++){
        if(s[k]=='1'){
            i++;
        }
    }
    return i;
}


int getResult(int i){
    return bottom[i]*numberOfOnes(toBinaryString(b[b.size() - 1-i]));
}

int main()
{
    

    getline(cin, firstLine);
    getline(cin, scndLine);

    istringstream f(firstLine);
    istringstream ff(scndLine);
    string s;

    while (getline(f, s, ' ')) {
        top.push_back(stoi(s));
    }
  
    while (getline(ff, s, ' ')) {
    bottom.push_back(stoi(s));
    }   
    getChainB();
    int result =0;

    for(int i=0; i<b.size(); i++){
        result += getResult(i);
    }

    cout << result << "\n";


	return 0;
}