#include <iostream> #include <vector> #include <set> #include <bitset> #include <algorithm> #define MIN_VALUE -400000000000000000ll class Solution { public: Solution(long long N) : N(N), A(N) {} void run() { std::cin >> M; for(auto& a : A) std::cin >> a; if(M <= 10000000) smallM(); else bigM(); } void smallM() { std::vector<long long> dp = std::vector<long long>(N, MIN_VALUE); long long m = 0; for(;m<N;++m) { for(long long n = m ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } for(;m<=M;++m) { for(long long n = N-1 ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } std::cout << dp[N-1] << std::endl; } void bigM() { std::vector<long long> dp = std::vector<long long>(N, MIN_VALUE); long long m = 0; for(;m<N;++m) { for(long long n = m ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } generateMSet(); for(const auto& m : Mset) { for(long long n = N-1 ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } std::cout << dp[N-1] << std::endl; } void generateMSet() { long long small = 2; long long big = 1; while(big <= M+1) big <<= 1; big >>= 1; while(Mset.size() < 1000000) // 10e6 { long long newSmallNumber = small -1; while(newSmallNumber < N) newSmallNumber = nextHigherWithSameA1(newSmallNumber); while(newSmallNumber <= M) { Mset.insert(newSmallNumber); newSmallNumber = nextHigherWithSameA1(newSmallNumber); } long long newBigNumber = big - 1; while(newBigNumber < N) newBigNumber = nextHigherWithSameA1(newBigNumber); while(newBigNumber <= M) { Mset.insert(newBigNumber); newBigNumber = nextHigherWithSameA1(newBigNumber); } small <<=1; big >>=1; } } long long nextHigherWithSameA1(const long long& x) { long long smallest, ripple, ones; smallest = x & -x; ripple = x + smallest; ones = x ^ ripple; ones = (ones)/smallest; ones >>= 2; return ripple | ones; } long long N; long long M; std::vector<long long> A; std::set<long long> Mset; }; int main() { int N; std::cin >> N; Solution s(N); s.run(); return 0; }
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <iostream> #include <vector> #include <set> #include <bitset> #include <algorithm> #define MIN_VALUE -400000000000000000ll class Solution { public: Solution(long long N) : N(N), A(N) {} void run() { std::cin >> M; for(auto& a : A) std::cin >> a; if(M <= 10000000) smallM(); else bigM(); } void smallM() { std::vector<long long> dp = std::vector<long long>(N, MIN_VALUE); long long m = 0; for(;m<N;++m) { for(long long n = m ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } for(;m<=M;++m) { for(long long n = N-1 ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } std::cout << dp[N-1] << std::endl; } void bigM() { std::vector<long long> dp = std::vector<long long>(N, MIN_VALUE); long long m = 0; for(;m<N;++m) { for(long long n = m ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } generateMSet(); for(const auto& m : Mset) { for(long long n = N-1 ; n > 0 ; --n) { dp[n] = std::max(dp[n], A[n] * __builtin_popcount(m) + dp[n-1]); } dp[0] = std::max(dp[0], A[0] * __builtin_popcount(m)); } std::cout << dp[N-1] << std::endl; } void generateMSet() { long long small = 2; long long big = 1; while(big <= M+1) big <<= 1; big >>= 1; while(Mset.size() < 1000000) // 10e6 { long long newSmallNumber = small -1; while(newSmallNumber < N) newSmallNumber = nextHigherWithSameA1(newSmallNumber); while(newSmallNumber <= M) { Mset.insert(newSmallNumber); newSmallNumber = nextHigherWithSameA1(newSmallNumber); } long long newBigNumber = big - 1; while(newBigNumber < N) newBigNumber = nextHigherWithSameA1(newBigNumber); while(newBigNumber <= M) { Mset.insert(newBigNumber); newBigNumber = nextHigherWithSameA1(newBigNumber); } small <<=1; big >>=1; } } long long nextHigherWithSameA1(const long long& x) { long long smallest, ripple, ones; smallest = x & -x; ripple = x + smallest; ones = x ^ ripple; ones = (ones)/smallest; ones >>= 2; return ripple | ones; } long long N; long long M; std::vector<long long> A; std::set<long long> Mset; }; int main() { int N; std::cin >> N; Solution s(N); s.run(); return 0; } |