#include <iostream> using namespace std; int n, m; int * a; int * b; int * c; int maxJakosc(int k, int bk[]) { if (k == n) { int s = 0; for (int i = 0; i < n; i++) s += a[i] * c[bk[i]]; return s; } int max = 0; for (int i = bk[k - 1] + 1; i <= m; i++) { bk[k] = i; int nMax = maxJakosc(k + 1, bk); if (nMax > max) max = nMax; } return max; } int main() { cin >> n >> m; a = new int[n]; b = new int[n]; c = new int[m+1]; for (int i = 0; i < n; i++) cin >> a[i]; c[0] = 0; int d = 1; for (int i = 1; i <= m; i++) { if (i < d + d) c[i] = c[i - d] + 1; else { d += d; c[i] = 1; } } int max = 0; for (int i = 0; i <= m; i++) { b[0] = i; int nMax = maxJakosc(1, b); if (nMax > max) max = nMax; } cout << max << endl; delete[] a; delete[] b; delete[] c; 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 | #include <iostream> using namespace std; int n, m; int * a; int * b; int * c; int maxJakosc(int k, int bk[]) { if (k == n) { int s = 0; for (int i = 0; i < n; i++) s += a[i] * c[bk[i]]; return s; } int max = 0; for (int i = bk[k - 1] + 1; i <= m; i++) { bk[k] = i; int nMax = maxJakosc(k + 1, bk); if (nMax > max) max = nMax; } return max; } int main() { cin >> n >> m; a = new int[n]; b = new int[n]; c = new int[m+1]; for (int i = 0; i < n; i++) cin >> a[i]; c[0] = 0; int d = 1; for (int i = 1; i <= m; i++) { if (i < d + d) c[i] = c[i - d] + 1; else { d += d; c[i] = 1; } } int max = 0; for (int i = 0; i <= m; i++) { b[0] = i; int nMax = maxJakosc(1, b); if (nMax > max) max = nMax; } cout << max << endl; delete[] a; delete[] b; delete[] c; return 0; } |