You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Lothar Buchholz 84dbcf81ee use JUnit5 Extensions to redirect System.in and System.out and run benchmarks
added apache-commons libs
applied new code formatting rules
5 years ago
.idea use JUnit5 Extensions to redirect System.in and System.out and run benchmarks 5 years ago
doc Added description and testdata 5 years ago
gradle/wrapper Initial commit 5 years ago
src use JUnit5 Extensions to redirect System.in and System.out and run benchmarks 5 years ago
.gitignore Initial commit 5 years ago
README.md updated problem description, test cases and test class to match n-sales 5 years ago
build.gradle use JUnit5 Extensions to redirect System.in and System.out and run benchmarks 5 years ago
gradlew Initial commit 5 years ago
gradlew.bat Initial commit 5 years ago
settings.gradle updated problem description, test cases and test class to match n-sales 5 years ago

README.md

Buy and Sell n-times

Goal

Given is the price-development of an arbitrary stock.

stock-price over time

Suppose you have perfect knowledge and are able to buy and sell the stock multiple times. Write an algorithm that outputs the maximum profit that can be made by exactly n sales.

  • The price-development is given for an arbitrary number of days.
  • A stock can only be sold on the next day.
  • Stock-prices are given as integers
  • Each buy must be made on another date after the previous sale

Input

A blank-separated array of numbers.
The first number is the number of sales allowed.
All following numbers are stock-prices.

Each position symbolises the stock-price of a given day.

Output

The maximum profit that can be made within that period by n sales.

Constraints

  • 2 <= n <= 10
  • 2 * n <= days with stock-prices <= 50
  • 1 <= stock-price <= 2,147,483,647

Example

Input: 3 12 11 13 9 12 8 14 13 15
where n is 3

Output: 12