c++, dp
they can only meet at diagonal, if they cross diagonal they cannot reach end of matrix in n - 1 steps, since for firstchild to reach end of matrix in n - 1 step
they can only meet at diagonal, if they cross diagonal they cannot reach end of matrix in n - 1 steps, since for firstchild to reach end of matrix in n - 1 steps , child must travel through through diagonal, since child one collects all fruits in diagonal we restrict out other 2 child to above or below the diagonal;\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    // we can combine upper, lower into same matrix for less memory usage\n    int maxCollectedFruits(vector<vector<int>>& fruits) {\n        int cen = 0, n = fruits.size();\n//for first child\n        for(int i = 0; i < n; i++){\n            cen += fruits[i][i];\n        }\n        vector<vector<int>> upper(n + 1, vector<int>(n + 1, -1)), lower(n + 1, vector<int>(n + 1, -1));\n        upper[0][n - 1] = fruits[0][n - 1];\n        for(int i = 1; i < n; i++){\n            for(int j = n - 1; j >= i + 1; j--){\n                int m = max({upper[i - 1][j - 1], upper[i - 1][j + 1], upper[i - 1][j]});\n                // if m = -1 it means any of previous cells is not visited so we wont update it\n                if(m != -1)\n                upper[i][j] =  m + fruits[i][j];\n            }\n        }\n        lower[n - 1][0] = fruits[n - 1][0];\n        for(int j = 1; j < n; j++){\n            for(int i = n - 1; i >= j + 1; i--){\n                int m = max({lower[i + 1][j - 1], lower[i][j - 1], lower[i - 1][j - 1]});\n                // if m = -1 it means any of previous cells is not visited so we wont update it\n                if(m != -1){\n                lower[i][j] = m + fruits[i][j];\n                }\n            }\n        }\n        int i = n - 1, j = n - 1;\n        lower[i][j] = max({lower[i + 1][j - 1], lower[i][j - 1], lower[i - 1][j - 1]});\n        upper[i][j] = max({upper[i - 1][j - 1], upper[i - 1][j + 1], upper[i - 1][j]});\n        return cen + lower[n - 1][n - 1] + upper[n - 1][n - 1];\n\n    }\n};\n```
['C++']