#include<cstdio> #include<iostream> using namespace std; long long int score[201]; long long int worst_result = -(5 * 10e17); long long int counting = 0; long long int fun(long long int num) { int r = 0; while(num > 0) { if(num % 2 == 1) { r++; } num /= 2; } return r; } long long int len(long long int num) { int r = 0; while(num > 0) { r++; num /= 2; } return r; } long long int pref(long long int num1, long long int num2) { int r = 0; while(num1 > 0) { if(num1%2 != num2%2) { r = 0; } else { r += 1; } num1 /= 2; num2 /= 2; } return r; } long long int pref1(long long int num1, long long int num2) { int r = 0; while(num1 > 0) { if(num1%2 != num2%2) { r = 0; } else { if(num1%2 == 1){ r += 1; } } num1 /= 2; num2 /= 2; } return r; } long long int smallest(long long int minn, long long int maxx) { if(minn > maxx) { return 1000; } if(minn == maxx) { return fun(minn); } if(minn == 0) { return 0; } if(len(minn) == len(maxx)) { return min(fun(minn), pref1(minn, maxx)+1); } else { return 1; } } long long int biggest(long long int minn, long long int maxx) { if(minn > maxx) { return -1000; } if(minn == maxx) { return fun(minn); } if(len(maxx) == fun(maxx)) { return len(maxx); } if(len(minn) == len(maxx)) { return max(fun(maxx), ((len(minn)-pref(minn,maxx))-1+pref1(minn,maxx)) ); } else { return len(maxx)-1; } } long long int result(long long int minn, long long int maxx, int first, int last) { counting++; if(last-first > maxx-minn) { return worst_result; } if(first > last) { return 0; } if(minn > maxx) { return worst_result; } if(first == last) { if(score[first] < 0) { return smallest(minn, maxx)*score[first]; } else { return biggest(minn,maxx)*score[first]; } } long long int midd = (minn+maxx+1)/2; long long int res = worst_result; for (int i = first-1; i <= last; i++) { res = max(res, result(minn, midd-1, first, i)+result(midd, maxx, i+1, last)); } return res; } int main() { ios_base::sync_with_stdio(0); long long int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> score[i]; } cout << result(0, k, 1, n) << endl; 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 | #include<cstdio> #include<iostream> using namespace std; long long int score[201]; long long int worst_result = -(5 * 10e17); long long int counting = 0; long long int fun(long long int num) { int r = 0; while(num > 0) { if(num % 2 == 1) { r++; } num /= 2; } return r; } long long int len(long long int num) { int r = 0; while(num > 0) { r++; num /= 2; } return r; } long long int pref(long long int num1, long long int num2) { int r = 0; while(num1 > 0) { if(num1%2 != num2%2) { r = 0; } else { r += 1; } num1 /= 2; num2 /= 2; } return r; } long long int pref1(long long int num1, long long int num2) { int r = 0; while(num1 > 0) { if(num1%2 != num2%2) { r = 0; } else { if(num1%2 == 1){ r += 1; } } num1 /= 2; num2 /= 2; } return r; } long long int smallest(long long int minn, long long int maxx) { if(minn > maxx) { return 1000; } if(minn == maxx) { return fun(minn); } if(minn == 0) { return 0; } if(len(minn) == len(maxx)) { return min(fun(minn), pref1(minn, maxx)+1); } else { return 1; } } long long int biggest(long long int minn, long long int maxx) { if(minn > maxx) { return -1000; } if(minn == maxx) { return fun(minn); } if(len(maxx) == fun(maxx)) { return len(maxx); } if(len(minn) == len(maxx)) { return max(fun(maxx), ((len(minn)-pref(minn,maxx))-1+pref1(minn,maxx)) ); } else { return len(maxx)-1; } } long long int result(long long int minn, long long int maxx, int first, int last) { counting++; if(last-first > maxx-minn) { return worst_result; } if(first > last) { return 0; } if(minn > maxx) { return worst_result; } if(first == last) { if(score[first] < 0) { return smallest(minn, maxx)*score[first]; } else { return biggest(minn,maxx)*score[first]; } } long long int midd = (minn+maxx+1)/2; long long int res = worst_result; for (int i = first-1; i <= last; i++) { res = max(res, result(minn, midd-1, first, i)+result(midd, maxx, i+1, last)); } return res; } int main() { ios_base::sync_with_stdio(0); long long int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> score[i]; } cout << result(0, k, 1, n) << endl; return 0; } |