#include <stdio.h> #include <stdlib.h> #include <math.h> long long min = 0; long long max = 0; long long maxt[200]; double log2(long long x) { return log(x)/log(2); } int countSetBits(long long n) { int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } long long calculateQuality(short n, long long a[200],long long b[200]) { long long w = 0; for (int i = 0; i < n; i++) w+=(a[i]*countSetBits(b[i])); return w; } long long findQuality(short n,long long a[200],long long limit) { //printf("findQuality: n=%hd, a=%lld, limit=%lld \n", n, a[n-1], limit); if (limit == 0) { if (n==1) return 0; else return min; } if (limit < n - 1) return min; int maxb = log2(limit+1); if (n == 1) { if (a[n-1] > 0) return a[n-1]*maxb; else return 0; } long long w = min; if (a[n-1] == 0) { w = findQuality(n-1,a,limit-1); } if (a[n-1] > 0) { int mb = maxb; long long lpmax = -1; long long maxSong = min; while (mb > 0) { long long lp = limit; while (lp > 0 && countSetBits(lp) != mb) lp--; lp--; if (lp > lpmax) { long long w1 = a[n-1]*mb; if (w1+maxt[n-2] > maxSong) { //printf(" go n=%hd, lp=%lld \n ",n-1,lp); long long wp = findQuality(n-1,a,lp); if (w1 + wp > w) w = w1 + wp; lpmax = lp; if (w1+wp > maxSong) maxSong = w1+wp; } } mb--; } } if (a[n-1] < 0) { int mb = 1; long long lpmax = -1; long long maxSong = min; while (mb <= maxb) { long long lp = limit; while (lp > 0 && countSetBits(lp) != mb) lp--; lp--; if (lp > lpmax) { long long w1 = a[n-1]*mb; if (w1+maxt[n-2] > maxSong) { //printf(" go n=%hd, lp=%lld \n ",n-1,lp); long long wp = findQuality(n-1,a,lp); if (w1 + wp > w) w = w1 + wp; lpmax = lp; if (w1+wp > maxSong) maxSong = w1+wp; } } mb++; } } //printf("findQuality: n=%hd, a=%lld, limit=%lld, wynik=%lld \n", n, a[n-1], limit, w); return w; } int main() { short n; // Song length long long m; // bits limit long long a[200]; // Song long long b[200]; // Bits long long quality; // Quality song // Read n and k scanf("%hd %lld",&n,&m); int maxb = log2(m+1); //printf("Max bits %i\n",maxb); // Read song for (int i = 0; i < n; i++) { scanf("%lld",&a[i]); if (a[i] > 0) { max+=(a[i]*maxb); } if (a[i] < 0) { if (i>0) max+=a[i]; min+=(a[i]*maxb); } maxt[i] = max; } //for (int i = 0; i < n; i++) // printf("%lld ",a[i]); //printf("\n"); //printf("Min %lld, Max %lld \n",min,max); //for (int i = 0; i < n; i++) // printf("%lld ",maxt[i]); //printf("\n"); quality = findQuality(n,a,m); printf("%lld",quality); 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 | #include <stdio.h> #include <stdlib.h> #include <math.h> long long min = 0; long long max = 0; long long maxt[200]; double log2(long long x) { return log(x)/log(2); } int countSetBits(long long n) { int count = 0; while (n) { count += n & 1; n >>= 1; } return count; } long long calculateQuality(short n, long long a[200],long long b[200]) { long long w = 0; for (int i = 0; i < n; i++) w+=(a[i]*countSetBits(b[i])); return w; } long long findQuality(short n,long long a[200],long long limit) { //printf("findQuality: n=%hd, a=%lld, limit=%lld \n", n, a[n-1], limit); if (limit == 0) { if (n==1) return 0; else return min; } if (limit < n - 1) return min; int maxb = log2(limit+1); if (n == 1) { if (a[n-1] > 0) return a[n-1]*maxb; else return 0; } long long w = min; if (a[n-1] == 0) { w = findQuality(n-1,a,limit-1); } if (a[n-1] > 0) { int mb = maxb; long long lpmax = -1; long long maxSong = min; while (mb > 0) { long long lp = limit; while (lp > 0 && countSetBits(lp) != mb) lp--; lp--; if (lp > lpmax) { long long w1 = a[n-1]*mb; if (w1+maxt[n-2] > maxSong) { //printf(" go n=%hd, lp=%lld \n ",n-1,lp); long long wp = findQuality(n-1,a,lp); if (w1 + wp > w) w = w1 + wp; lpmax = lp; if (w1+wp > maxSong) maxSong = w1+wp; } } mb--; } } if (a[n-1] < 0) { int mb = 1; long long lpmax = -1; long long maxSong = min; while (mb <= maxb) { long long lp = limit; while (lp > 0 && countSetBits(lp) != mb) lp--; lp--; if (lp > lpmax) { long long w1 = a[n-1]*mb; if (w1+maxt[n-2] > maxSong) { //printf(" go n=%hd, lp=%lld \n ",n-1,lp); long long wp = findQuality(n-1,a,lp); if (w1 + wp > w) w = w1 + wp; lpmax = lp; if (w1+wp > maxSong) maxSong = w1+wp; } } mb++; } } //printf("findQuality: n=%hd, a=%lld, limit=%lld, wynik=%lld \n", n, a[n-1], limit, w); return w; } int main() { short n; // Song length long long m; // bits limit long long a[200]; // Song long long b[200]; // Bits long long quality; // Quality song // Read n and k scanf("%hd %lld",&n,&m); int maxb = log2(m+1); //printf("Max bits %i\n",maxb); // Read song for (int i = 0; i < n; i++) { scanf("%lld",&a[i]); if (a[i] > 0) { max+=(a[i]*maxb); } if (a[i] < 0) { if (i>0) max+=a[i]; min+=(a[i]*maxb); } maxt[i] = max; } //for (int i = 0; i < n; i++) // printf("%lld ",a[i]); //printf("\n"); //printf("Min %lld, Max %lld \n",min,max); //for (int i = 0; i < n; i++) // printf("%lld ",maxt[i]); //printf("\n"); quality = findQuality(n,a,m); printf("%lld",quality); return 0; } |