#include <vector> #include <iostream> #include <algorithm> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvc = vector<vc>; using vvi = vector<vi>; using vvb = vector<vb>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vvll = vector<vll>; using vull = vector<ull>; #define f first #define s second #define pb emplace_back #define sz size() #define graph vector<vertex> #define rep(i, begin, end) for(__typeof(end) i = (begin); i <= (end); ++i) #define repr(i, begin, end) for(__typeof(end) i = (begin); i >= (end); --i) #define bend(X) X.begin(), X.end() #define print cout<< #ifdef LOCAL #define nl << endl #define deb(x) cout << #x << " = " << x << endl #define say(x) cout << x << endl #else #define nl << '\n' #define deb(x) #define say(x) #endif constexpr int INF = 2000000000; constexpr ll INFl = 2000000000ll; constexpr ll INFll = 2000000000000000000ll; constexpr ll mod = 1e9+7; struct num { num(ll val = 0) : v{val} {if(v>=mod) v %= mod;}; ll v; operator ll() {return v;}; num operator+(const num& o) const {return {v+o.v};}; num operator-(const num& o) const {return {v-o.v+mod};}; num operator*(const num& o) const {return {v*o.v};}; num operator+=(const num& o) {v = num{v+o.v}; return *this;}; num operator-=(const num& o) {v = num{v-o.v+mod}; return *this;}; num operator*=(const num& o) {v = num{v*o.v}; return *this;}; num operator!() const { num R = 1; rep(i, 2, v+1) R *= num{i}; return R; } num operator^(ll b) const { num R{1}, a{v}; while(b > 0) { if(b&1) R *= a; a *= a, b /= 2; } return R; } num operator/(const num& o) const { return (*this)*(o^(mod-2)); } }; template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2> x) { os << '(' << x.f << ' ' << x.s << ')'; return os; } template <typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& x) { is >> x.f >> x.s; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& x) { for(T& i : x) os << i << ' '; return os; } template <typename T> istream& operator>>(istream& is, vector<T>& x) { for(T& i : x) is >> i; return is; } template <typename T1, typename T2> pair<T1,T2> operator+(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f+B.f, A.s+B.s}; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f-B.f, A.s-B.s}; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A) { return {-A.f, -A.s}; } template<typename T1, typename T2> bool maxe(T1& A, const T2& B) {if(B>A){A=B;return 1;}return 0;} template<typename T1, typename T2> bool mine(T1& A, const T2& B) {if(B<A){A=B;return 1;}return 0;} // // END OF DEFINES // int n; ll m; vll T; vvll DP, MX; bool in() { cin >> n >> m; T.resize(n+1); DP.resize(n+1, vll(m+1)); MX.resize(n+1, vll(m+1)); rep(i, 1, n) cin >> T[i]; return true; } inline int C(ll x) { return __builtin_popcountll(x); } ll solve() { rep(j, 0, m) DP[1][j] = C(j)*T[1]; MX[1][0] = DP[1][0]; rep(j, 1, m) MX[1][j] = max(MX[1][j-1], DP[1][j]); rep(i, 2, n) { DP[i][0] = -INFll; MX[i][0] = -INFll; rep(j, 1, m) { DP[i][j] = MX[i-1][j-1]; if(DP[i][j] > -INFll) DP[i][j] += T[i]*C(j); MX[i][j] = max(DP[i][j], MX[i][j-1]); } } return MX[n][m]; return -INF; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int noOfTests = 1; //cin >> noOfTests; while(noOfTests --> 0) { if(!in()) { cout << "popsute_wejscie\n"; continue; } ll result = solve(); if(result == ll(-'t')) cout << "TAK\n"; else if(result == ll(-'n')) cout << "NIE\n"; else if(result == ll(-'e')) cout << "Popsute\n"; else if(result != -INF) cout << result << '\n'; } 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <vector> #include <iostream> #include <algorithm> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vb = vector<bool>; using vc = vector<char>; using vvc = vector<vc>; using vvi = vector<vi>; using vvb = vector<vb>; using vpii = vector<pii>; using vpll = vector<pll>; using vll = vector<ll>; using vvll = vector<vll>; using vull = vector<ull>; #define f first #define s second #define pb emplace_back #define sz size() #define graph vector<vertex> #define rep(i, begin, end) for(__typeof(end) i = (begin); i <= (end); ++i) #define repr(i, begin, end) for(__typeof(end) i = (begin); i >= (end); --i) #define bend(X) X.begin(), X.end() #define print cout<< #ifdef LOCAL #define nl << endl #define deb(x) cout << #x << " = " << x << endl #define say(x) cout << x << endl #else #define nl << '\n' #define deb(x) #define say(x) #endif constexpr int INF = 2000000000; constexpr ll INFl = 2000000000ll; constexpr ll INFll = 2000000000000000000ll; constexpr ll mod = 1e9+7; struct num { num(ll val = 0) : v{val} {if(v>=mod) v %= mod;}; ll v; operator ll() {return v;}; num operator+(const num& o) const {return {v+o.v};}; num operator-(const num& o) const {return {v-o.v+mod};}; num operator*(const num& o) const {return {v*o.v};}; num operator+=(const num& o) {v = num{v+o.v}; return *this;}; num operator-=(const num& o) {v = num{v-o.v+mod}; return *this;}; num operator*=(const num& o) {v = num{v*o.v}; return *this;}; num operator!() const { num R = 1; rep(i, 2, v+1) R *= num{i}; return R; } num operator^(ll b) const { num R{1}, a{v}; while(b > 0) { if(b&1) R *= a; a *= a, b /= 2; } return R; } num operator/(const num& o) const { return (*this)*(o^(mod-2)); } }; template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2> x) { os << '(' << x.f << ' ' << x.s << ')'; return os; } template <typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& x) { is >> x.f >> x.s; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& x) { for(T& i : x) os << i << ' '; return os; } template <typename T> istream& operator>>(istream& is, vector<T>& x) { for(T& i : x) is >> i; return is; } template <typename T1, typename T2> pair<T1,T2> operator+(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f+B.f, A.s+B.s}; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A, const pair<T1,T2>& B) { return {A.f-B.f, A.s-B.s}; } template <typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A) { return {-A.f, -A.s}; } template<typename T1, typename T2> bool maxe(T1& A, const T2& B) {if(B>A){A=B;return 1;}return 0;} template<typename T1, typename T2> bool mine(T1& A, const T2& B) {if(B<A){A=B;return 1;}return 0;} // // END OF DEFINES // int n; ll m; vll T; vvll DP, MX; bool in() { cin >> n >> m; T.resize(n+1); DP.resize(n+1, vll(m+1)); MX.resize(n+1, vll(m+1)); rep(i, 1, n) cin >> T[i]; return true; } inline int C(ll x) { return __builtin_popcountll(x); } ll solve() { rep(j, 0, m) DP[1][j] = C(j)*T[1]; MX[1][0] = DP[1][0]; rep(j, 1, m) MX[1][j] = max(MX[1][j-1], DP[1][j]); rep(i, 2, n) { DP[i][0] = -INFll; MX[i][0] = -INFll; rep(j, 1, m) { DP[i][j] = MX[i-1][j-1]; if(DP[i][j] > -INFll) DP[i][j] += T[i]*C(j); MX[i][j] = max(DP[i][j], MX[i][j-1]); } } return MX[n][m]; return -INF; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int noOfTests = 1; //cin >> noOfTests; while(noOfTests --> 0) { if(!in()) { cout << "popsute_wejscie\n"; continue; } ll result = solve(); if(result == ll(-'t')) cout << "TAK\n"; else if(result == ll(-'n')) cout << "NIE\n"; else if(result == ll(-'e')) cout << "Popsute\n"; else if(result != -INF) cout << result << '\n'; } return 0; } |