Investing in community care saves money for the wider NHS, analysis shows