#include <iostream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <set>
#include <memory>

struct TestData
{
  int testNum;
  std::string input;
  std::string expectedOutput;
};

struct TestResult
{
  std::string testOutput;
  bool testPassed;
};

void testSolution();
TestResult runTestCase(const TestData& testData);
std::string formatResult(const TestData& testData, const TestResult& testResult);
std::string formatOverallResult(const std::vector<TestResult>& testResult);
void testFunctions();
bool countLongestConsecutiveLEThanFromRight_ShouldReturn3WhenAllSameNum();
bool countLongestConsecutiveLEThanFromRight_ShouldReturn0WhenIncreasingSequence();
int countLongestConsecutiveLEThanFromRight(const std::vector<int>& heights);
void solution(std::istream& is, std::ostream& os);

static const bool IS_TESTING = false;

// define test data
static const std::vector<TestData> testDatas = {
  {1, "5\n10 4 8 7 3\n", "2\n"},
  {2, "7\n4 4 5 6 6 5 5\n", "3\n"},
  {3, "4\n1 2 3 4\n", "0\n"},
};

int main()
{
  if (IS_TESTING) {
    testSolution();
    testFunctions();
  }
  else
    solution(std::cin, std::cout);

  return 0;
}

void testSolution()
{
  std::cout << "Testing solution..." << std::endl;
  
  auto results = std::vector<TestResult>();
  for (const auto& testData : testDatas) {
    auto testResult = runTestCase(testData);
    results.push_back(testResult);

    std::cout << formatResult(testData, testResult);
  }
  std::cout << formatOverallResult(results);
}

TestResult runTestCase(const TestData& testData)
{
    std::istringstream iss {testData.input};
    std::ostringstream oss {};

    solution(iss, oss);

    auto testOutput = oss.str();
    auto testPassed = testData.expectedOutput.compare(testOutput) == 0;
    
    return {testOutput, testPassed};
}

std::string formatResult(const TestData& testData, const TestResult& testResult)
{
  std::ostringstream oss {};

  oss << "Test " << std::setw(2) << testData.testNum << ": "
    << (testResult.testPassed ? "Passed!" : "Failed!") << std::endl;
  
  oss << "\tExpected: " << testData.expectedOutput << "\tActual  : " << testResult.testOutput << std::endl;

  return oss.str();
}

std::string formatOverallResult(const std::vector<TestResult>& testResults)
{
  std::ostringstream oss {};

  int passedTestCount = 0;
  for (const auto& result : testResults) {
    if (result.testPassed) passedTestCount++;
  }

  oss << passedTestCount << "/" << testResults.size()
    << " tests passed" << std::endl;

  return oss.str();
}

void testFunctions()
{
  std::cout << "Testing functions..." << std::endl;
  std::vector<bool (*)()> tests = {
    countLongestConsecutiveLEThanFromRight_ShouldReturn3WhenAllSameNum,
    countLongestConsecutiveLEThanFromRight_ShouldReturn0WhenIncreasingSequence,
  };

  int numPassed = 0;
  for (auto i = 0; i < tests.size(); ++i) {
    auto isPassed = tests[i]();
    if (isPassed) numPassed++;
    std::cout << "Test " << std::setw(2) << i << ": "
      << (isPassed ? "Passed!\n" : "Failed\n");
  }
}

bool countLongestConsecutiveLEThanFromRight_ShouldReturn3WhenAllSameNum()
{
  std::vector<int> heights = {2, 2, 2, 2};
  auto expected = 3;

  auto result = countLongestConsecutiveLEThanFromRight(heights);
  return expected == result;
}

bool countLongestConsecutiveLEThanFromRight_ShouldReturn0WhenIncreasingSequence()
{
  std::vector<int> heights = {1, 2, 3, 4};
  auto expected = 0;

  auto result = countLongestConsecutiveLEThanFromRight(heights);
  return expected == result;
}

int countLongestConsecutiveLEThanFromRight(const std::vector<int>& heights)
{
  auto consecutiveCount = 0;
  auto longestConsecutive = 0;
  auto currentIdx = heights.size() - 1;
 
  while (currentIdx > 0) {
    auto compareAgainst = currentIdx - 1;

    if (heights[currentIdx] <= heights[compareAgainst]) {
      ++consecutiveCount;
      longestConsecutive  = std::max({consecutiveCount, longestConsecutive});
    }
    else
      consecutiveCount = 0;
    
    --currentIdx;
  }

  return longestConsecutive;
}

// solution to the problem
void solution(std::istream& is, std::ostream& os)
{
  // read from input
  int numCount; is >> numCount;
  auto heights = std::vector<int>(numCount);
  for (auto i = 0; i < heights.size(); ++i) {
    is >> heights[i];
  }

  os << countLongestConsecutiveLEThanFromRight(heights) << std::endl;
}

Measured memory_bytes: 640