Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- namespace Contest1
- {
- class Program
- {
- //private static int[] costs;
- //private static int n, k;
- private static int[] costs = { 2, -3, 5 };
- private static int n = 5;
- private static int k = 3;
- static void Main(string[] args)
- {
- GetData();
- GetAnswer();
- }
- private static void GetData()
- {
- using (StreamReader sr = new StreamReader("input.txt"))
- {
- string[] constants = sr.ReadLine().Split(' ');
- int.TryParse(constants[0], out n);
- int.TryParse(constants[1], out k);
- costs = sr.ReadLine().Replace("\n", "").Split(' ').Select(x => int.Parse(x)).ToArray();
- }
- }
- static void GetAnswer()
- {
- List<int> jumps = new List<int>() { 0, 0 };
- List<int> results = new List<int>() { 0, 0 };
- for (int i = 2; i <= n; i++)
- {
- Tuple<int, int> best_variant = new Tuple<int, int>(i - 1, results[i - 1]);
- int j = 1;
- while (j <= k && i - j > 0)
- {
- if (results[i - j] > best_variant.Item2)
- {
- best_variant = new Tuple<int, int>(i - j, results[i - j]);
- }
- j++;
- }
- jumps.Add(best_variant.Item1);
- results.Add(i - 2 < costs.Length ? best_variant.Item2 + costs[i - 2] : best_variant.Item2);
- }
- int counter = 0;
- int l = results.Count - 1;
- List<int> way = new List<int>() { l };
- while (l > 1)
- {
- l = jumps[l];
- way.Add(l);
- counter++;
- }
- way.Reverse();
- using (StreamWriter sw = new StreamWriter("output.txt"))
- {
- sw.WriteLine(results.Last());
- sw.WriteLine(counter);
- sw.WriteLine(string.Join(" ", way));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement