Advertisement
Guest User

Untitled

a guest
Mar 21st, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         int[] array = Array.ConvertAll(Console.ReadLine().Split(' '), x => int.Parse(x));
  9.         int len = int.Parse(Console.ReadLine());
  10.         Solve(array, len);
  11.     }
  12.  
  13.     static void Solve(int[] arr, int len)
  14.     {
  15.         List<int> bestPriceArray = new List<int>();
  16.         int bestPrice = 0;
  17.         for (int i = 1; i <= len; i++)
  18.         {
  19.             int partsToSplitWith = i;
  20.             int lenNumber = len;
  21.             List<int> result = new List<int>();
  22.             int currentPrice = 0;
  23.             while (true)
  24.             {
  25.                 if (lenNumber - partsToSplitWith < 0)
  26.                 {
  27.                     partsToSplitWith -= 1;
  28.                 }
  29.  
  30.                 if (lenNumber - partsToSplitWith == 0)
  31.                 {
  32.                     result.Insert(0, partsToSplitWith);
  33.                     currentPrice += arr[partsToSplitWith];
  34.                     break;
  35.                 }
  36.  
  37.                 if (lenNumber - partsToSplitWith > 0)
  38.                 {
  39.                     result.Insert(0, partsToSplitWith);
  40.                     currentPrice += arr[partsToSplitWith];
  41.                     lenNumber -= partsToSplitWith;
  42.                 }
  43.             }
  44.  
  45.             // console.log(result.join(" ") + " currentPrice: " + currentPrice);
  46.             if (bestPrice < currentPrice)
  47.             {
  48.                 bestPrice = currentPrice;
  49.                 bestPriceArray.Clear();
  50.                 for (int t = 0; t < result.Count; t++)
  51.                 {
  52.                     bestPriceArray.Add(result[t]);
  53.                 }
  54.             }
  55.         }
  56.  
  57.         Console.WriteLine(bestPrice);
  58.         for (int t = 0; t < bestPriceArray.Count; t++)
  59.         {
  60.             if (t >= bestPriceArray.Count - 1)
  61.             {
  62.                 Console.Write(bestPriceArray[t]);
  63.             }
  64.             else
  65.             {
  66.                 Console.Write(bestPriceArray[t] + " ");
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement