Задача на динамическое программирование. Советую почитать поподробнее на эту тему в интернете. Подобные задачи для изучения: платная лестница, кузнечик, последовательность Фибоначчи. #include <iostream> #include <cmath> using namespace std; int main() { int n; cin >> n; int height[n]; for(int i=0; i<n; i++) cin >> height[i]; int energy[n]; for(int i=1; i<n; i++) energy[i] = energy[i-1] + abs(height[i]-height[i-1]); cout << height[n-1]; return 0; }
Sub m1() Cells.Clear Dim n As Integer, i As Integer, s As Integer n = InputBox("n = ") Cells(1, 1).Value = "n = " + Str(n) ReDim a(1 To n) For i = 1 To n a(i) = InputBox("a(" + Str(i) + ")") Next i Cells(2, 1).Value = "Исходный массив" Range(Cells(3, 1), Cells(3, n)).Value = a s = 0 For i = 1 To n s = s + a(i) Next i Cells(4, 1).Value = "s = " + Str(s) Cells(5, 1).Value = "Полученный массив" For i = 1 To n a(i) = a(i) + s Next i Range(Cells(6, 1), Cells(6, n)).Value = a End Sub