D. Bookshelves

Codeforces
IDCF981D
Time1000ms
Memory256MB
Difficulty
bitmasksdpgreedy
English · Original
Chinese · Translation
Formal · Original
Mr Keks is a typical white-collar in Byteland. He has a bookshelf in his office with some books on it, each book has an integer positive price. Mr Keks defines the value of a shelf as the sum of books prices on it. Miraculously, Mr Keks was promoted and now he is moving into a new office. He learned that in the new office he will have not a single bookshelf, but exactly $k$ bookshelves. He decided that the beauty of the $k$ shelves is the [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND) of the values of all the shelves. He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on $k$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty. ## Input The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 50$) — the number of books and the number of shelves in the new office. The second line contains $n$ integers $a_1, a_2, \ldots a_n$, ($0 < a_i < 2^{50}$) — the prices of the books in the order they stand on the old shelf. ## Output Print the maximum possible beauty of $k$ shelves in the new office. [samples] ## Note In the first example you can split the books as follows: (9 + 14 + 28 + 1 + 7) \\& (13 + 15) \\& (29 + 2) \\& (31) = 24. In the second example you can split the books as follows: (3 + 14 + 15 + 92) \\& (65) \\& (35 + 89) = 64.
Mr Keks 是 Byteland 的一名典型白领。 他的办公室里有一个书架,上面放了一些书,每本书都有一个正整数价格。 Mr Keks 将一个书架的值定义为该书架上所有书的价格之和。 奇迹般地,Mr Keks 被提拔了,现在他要搬进一个新办公室。 他得知在新办公室里,他将不再只有一个书架,而是恰好有 $k$ 个书架。他决定这 $k$ 个书架的美观度是所有书架值的按位与(bitwise AND)。 他还决定不会花时间重新整理书籍,因此他会将前若干本书放在第一个书架上,接下来的若干本书放在第二个书架上,依此类推。当然,每个书架上至少要放一本书。这样,他会将所有书分配到 $k$ 个书架上,使得书架的美观度尽可能大。请计算这个最大可能的美观度。 第一行包含两个整数 $n$ 和 $k$($1 lt.eq k lt.eq n lt.eq 50$)—— 书的数量和新办公室中书架的数量。 第二行包含 $n$ 个整数 $a_1, a_2, dots.h a_n$($0 < a_i < 2^(50)$)—— 按照旧书架上顺序排列的书的价格。 请输出新办公室中 $k$ 个书架的最大可能美观度。 在第一个例子中,你可以将书分配如下: $$(9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.$$ 在第二个例子中,你可以将书分配如下: $$(3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.$$ ## Input 第一行包含两个整数 $n$ 和 $k$($1 lt.eq k lt.eq n lt.eq 50$)—— 书的数量和新办公室中书架的数量。第二行包含 $n$ 个整数 $a_1, a_2, dots.h a_n$($0 < a_i < 2^(50)$)—— 按照旧书架上顺序排列的书的价格。 ## Output 请输出新办公室中 $k$ 个书架的最大可能美观度。 [samples] ## Note 在第一个例子中,你可以将书分配如下:$$(9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.$$在第二个例子中,你可以将书分配如下:$$(3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.$$
**Definitions** Let $ n, k \in \mathbb{Z} $ with $ 1 \leq k \leq n \leq 50 $. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of positive integers, where $ 0 < a_i < 2^{50} $. A **partition** of $ A $ into $ k $ contiguous non-empty shelves is a sequence of indices $ 0 = i_0 < i_1 < i_2 < \dots < i_k = n $, such that the $ j $-th shelf contains books $ a_{i_{j-1}+1}, \dots, a_{i_j} $ for $ j = 1, \dots, k $. Let $ S_j = \sum_{i=i_{j-1}+1}^{i_j} a_i $ be the value (sum) of the $ j $-th shelf. The **beauty** of the partition is defined as: $$ B = S_1 \& S_2 \& \cdots \& S_k $$ where $ \& $ denotes the bitwise AND operation. **Constraints** 1. $ 1 \leq k \leq n \leq 50 $ 2. $ a_i \in \mathbb{Z}^+ $, $ a_i < 2^{50} $ for all $ i \in \{1, \dots, n\} $ 3. Each shelf must contain at least one book: $ i_j - i_{j-1} \geq 1 $ for all $ j \in \{1, \dots, k\} $ **Objective** Maximize the beauty: $$ \max_{\text{valid partitions}} \left( \bigwedge_{j=1}^k S_j \right) $$
Samples
Input #1
10 4
9 14 28 1 7 13 15 29 2 31
Output #1
24
Input #2
7 3
3 14 15 92 65 35 89
Output #2
64
API Response (JSON)
{
  "problem": {
    "name": "D. Bookshelves",
    "description": {
      "content": "Mr Keks is a typical white-collar in Byteland. He has a bookshelf in his office with some books on it, each book has an integer positive price. Mr Keks defines the value of a shelf as the sum of boo",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF981D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Mr Keks is a typical white-collar in Byteland.\n\nHe has a bookshelf in his office with some books on it, each book has an integer positive price.\n\nMr Keks defines the value of a shelf as the sum of boo...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Mr Keks 是 Byteland 的一名典型白领。\n\n他的办公室里有一个书架,上面放了一些书,每本书都有一个正整数价格。\n\nMr Keks 将一个书架的值定义为该书架上所有书的价格之和。\n\n奇迹般地,Mr Keks 被提拔了,现在他要搬进一个新办公室。\n\n他得知在新办公室里,他将不再只有一个书架,而是恰好有 $k$ 个书架。他决定这 $k$ 个书架的美观度是所有书架值的按位与(bitwise ...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, k \\in \\mathbb{Z} $ with $ 1 \\leq k \\leq n \\leq 50 $.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of positive integers, where $ 0 < a_i < 2^{50} $.  \n\nA **partition** ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments