Codeforces 834 D. The bakery (segment tree +DP) __ Dynamic programming

Source: Internet
Author: User
Tags pack
Description

Some time ago Slastyona the Sweetmaid decided to open her own She bought required ingredients and a wonder-oven which can bake several types of cakes, and opened the bakery.

Soon the expenses started to overcome the income, so Slastyona decided to study the sweets. She learned it's profitable to pack cakes in boxes, and that's more distinct cake the types a box contains (let's denote thi S number as the value of the box), the higher price it has.

She needs to the production technology! The problem is this oven chooses the cake types on its own and Slastyona can ' t affect it. However, she knows the types and order of n cakes the oven are going to bake today. Slastyona has to pack exactly k boxes with cakes-today, and she has to put in each box several (at least one) cakes the OV En produced one right over another (in the other words, she has into put in a box a continuous segment of cakes).

Slastyona wants to maximize the total value of all boxes with cakes. Help her determine this maximum possible total value.

Input

The contains two integers n and K (1≤n≤35000, 1≤k≤min (n, 50)) –the number of cakes and the number of B Oxes, respectively.

The second line contains n integers a1, a2, ..., an (1≤ai≤n) –the the types of cakes to the order the oven bakes.

Output

Print the only integer–the maximum total value is boxes with cakes.

examples Input

4 1
1 2 2 1

examples Output

2

the

There are n n numbers divided into k K K, each segment of the value of which is the number of different numbers, to find the total maximum value.

train of Thought

It is easy to think of a DP mentality in which, Dp[i][k] D p [i] [K] dp[i][k] represents the maximum value that can be obtained by dividing the first I I numbers into K K K.

Obviously: when k=1 k = 1 k=1, dp[i][1] D p [i] [1] dp[i][1] for the different number of former I I I numbers when k>1 k > 1 k>1, DP[I][K]=MAX1≤J≤IDP [J−1] [K−1]+c[j][i] D p [i] [k] = max 1≤j≤i d p [j−1] [k−1] + c [j] [i] dp[i][k]=\max_{1≤j≤i}dp[j-1][k-1]+c[ J][i] (C[j][i] c [j] [i] c[j][i] represents the interval [j,i] [J, I] [j,i] The number of different numbers)

Calculate every value in a DP array the simple procedure requires the time of O (n) o (n) o (n), so that the total time complexity is O (k

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.